Linked List – Arithmetic – Problem #1

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

Difficulty: medium

Problem Statement

In a doubly linked list, each node is given two pointers, the first pointing to the next element in the list and the second to the previous element in the list. The next pointer of the last node and the previous pointer of the first node should be NULL. Describe a suitable C data type to store a node in a doubly linked list. Suppose that each node in the list stores an integer key value. Now, consider a sorted doubly linked list of integers in ascending order. • Write a C function to insert an integer ‘x’ in ‘L’ in the sorted order. If x is already present in L, then the insertion produces no change. If x is absent from L, it is inserted in the appropriate position so that after the insertion, L continues to remain sorted. • Write a C function to return the minimum search distance for a particular element within L. The minimum search distance for an element ‘x’ within ‘L’, if ‘x’ is present in ‘L’, refers to the minimum of the distances from the front and rear end of the list to the particular element ‘x’. For example, if the list contains (2,4,6,8,9,10), then the minimum search distance of element ‘9’ is 2, as distance from front end to ‘9’ = 5 and distance from rear end to ‘9’ = 2, and min(2,5) = 2. If ‘x’ is not present in ‘L’, then search distance returns -1. If there are duplicate elements ‘x’ within ‘L’, then the first occurrence of ‘x’ from both the sides should be considered as the respective distance and the minimum of that is returned. If the distance from both the left end and right end is the same (i.e the element is in the middle of an odd length list), then just that distance should be returned. Note that min(a,a) = a only. The search element should be taken as input from the keyboard and passed to the function as one of its arguments. You HAVE to use the concept of doubly linked in your logic for this function. • Write a function to print the sorted linked list starting from the head node. If the list is empty, then print ‘List empty’. For each of the above mentioned functions, write the function name, function definitions and function arguments as per your choice, but make sure that the functions perform the tasks as specified.and one of the arguments must be the head pointer of the linked list. In main(), write a menu-driven code that provides the user with four choices, insertion, finding minimum search distance, printing the list and exiting the program. Based on the user choice, the appropriate function is called. The menu is run continuously until the user wishes to exit. For insertion and search distance, the respective element is also taken as input. The linked list initially starts empty, with 0 number of nodes.

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

Lab Problem Statement

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

Interactive C Compiler · Problem #1

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.