Linked List – Arithmetic – Problem #3

IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive

Difficulty: hard

Problem Statement

You must have done online shopping where you can filter different items based on a number of parameters, one such being sorted as per the range of price of the item, i.e whether the price lies in the range, say, (0-500), then (501-1000), (1001-1500), (1501-2000) and so on. We can think of each range to be a bucket and within each bucket all the items whose price falls in the respective range are kept. In order to build such a filtering system, we can take the help of nested linked lists. As per the diagram shown, there is one outer linked list, and within each node of the outer list there is an inner linked list. Each node of the outer linked list denotes a bucket as per the price range in increasing order, starting from 0. It holds an integer variable ‘Range’ which denotes the upper limit of the price range for that bucket, the lower limit starting from after the previous bucket’s range. Overall the price starts from 0 (lower limit for the first bucket) and each successive bucket has a range of 500 units of currency. That is, if the value of ‘Range’ is 1000 for a bucket, this means that bucket holds all the items whose prices fall in the range 501 to 1000. The inner linked list denotes the list of all the individual items whose price falls within the range for that particular bucket. Each node of the inner list consists of two integer variables, ‘ItemID’ denoting the numerical ID of the item and ‘Price’, denoting the price of that particular item. Within each bucket, the inner list of items is also sorted in increasing order of their prices. Write a C program to build such a filtering system. • Define the suitable structure variable that can form such a nested linked list. In particular, the structure variable itself should be a nested structure to incorporate the outer and inner lists. • Initially, the entire list is empty, i.e no elements are present. Write a function void in- sert(Bucket *head ,int itemID, int Price), to insert a new item into the linked list as per the filtering order, where ‘Bucket *head’ points to the head of the outer linked list, ‘itemID’ refers to the ID of the particular item and ‘Price’ refers to the individual price of that item. You must first locate the appropriate bucket node of the outer list where the item has to be kept, then within that bucket you have to insert that item in the correct position of the inner list. If the appropriate bucket node is not present within the current list, then add a new bucket node that can hold that item, along with any intermediate bucket nodes that may be required. This is required because the buckets have a range of 500 only. The intermediate bucket nodes can hold an empty inner list, if no items for that bucket are present. Keep in mind that both the outer and inner lists are sorted in ascending order, and must remain sorted after each insertion. • From main(), take input the itemID and price of ‘n’ such items and insert them one by one within the filtered list by calling insert(). You may assume that for all items, minimum price is 0 and maximum price is 5000. • Finally after taking input is complete, print the entire list, as per the format below. Example sample output (refer to the diagram): Bucket 1: Lower limit: 0 Upper limit: 500 Items: (100,125) [NOTE: The item tuple is of the form (itemID,Price)] (110,450) Bucket 2: Lower limit: 501 Upper limit: 1000 Items: (150,670) (190,890) Bucket 3: Lower limit: 1001 Upper limit: 1500 Items: (200,1250) (130,1345)

Back to Lab Problems
Linked List/Arithmetic/Problem #3

Lab Problem Statement

Specifications & Problem DetailsIIT Kharagpur PDS Archive
Loading question...

Interactive C Compiler · Problem #3

WASM In-Browser

Runs directly on your machine · Zero remote server dependency

Compiler downloads on first Run (large, saved in your browser after that)
main.cCtrl + Enter to run
1
2
3
4
5
6
7
Input (stdin)given to scanf / cin
Output

Output appears here. Press Run to compile and execute.