Linked List – Arithmetic – Problem #2

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

Difficulty: hard

Problem Statement

Define a linked list where the head node sits in the middle and you can go either to the left or right of the node depending on the user input. The head node contains two pointer variables left and right to traverse either to the left or right of the node. All other nodes contain either the left pointer or the right pointer. Write a program to create such a linked list by inserting new nodes either to the left or to the right of the head node. Write a C program to perform the following tasks. • Let the user choose continuously from the keyboard (until ‘-1’ is pressed) the new element to be added and which direction the new insertion should happen. • Depending on the choice, create a new node containing either the left or right pointer. • Start from the head and traverse in the appropriate direction till the end of the respective left/right sub chain to insert a new node there. • At the end display both the left sub-chain and the right sub-chain. You should write two functions insertLeft(node* head, int data) and insertRight(node* head, int data) that inserts a new node either to the left or right of the head respectively. You can assume that the nodes contain only integer data. You can further assume that ’1’ represents left and ’0’ represents right as choice during user input. Example: Enter data: 10 Enter direction: 1 Enter data: 20 Enter direction: 0 Enter data: 30 Enter direction: 0 Enter data: 40 Enter direction: 1 Enter data: 50 Enter direction: 0 Enter data: -1 [NOTE: ‘-1’ is the exiting condition for taking further inputs] Output: Left linked list: 10 → 40 → NULL Right linked list: 20 → 30 → 50 → NULL [You have to print the arrow heads and the word ‘NULL’ to clearly represent the list]

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

Lab Problem Statement

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

Interactive C Compiler · Problem #2

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.