Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked - List Assignment 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

MOTILAL NEHRU NATIONAL INSTITUTE OF TECHNOLOGY ALLAHABAD

Data Structures (CSN32101), MCA (II Semester)

Additional Linked-list Assignment 2


(Last Date: 27/02/2024) CPI < 8.5
(Last Date 17/02/2024 CPI > 8.5
1. Create two circular linked lists and find their maximum numbers. Merge the two
circular linked lists such that the maximum number of 2nd circular linked list
immediately follows the maximum number of the 1st circular linked list.
Input: 12 -> 28 -> 18 -> 25 -> 19 -> NULL 5 -> 24 -> 12 -> 6 -> 15 -> NULL
Output: 12 -> 28 -> 24 -> 12 -> 6 -> 15 -> 5 -> 18 -> 25 -> 19 -> NULL

2. Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1) and l2 = (4,
5), after return from concatenate (l1, l2) the list l1 should be changed to be l1 = (2, 3,
1, 4, 5). Your function should not change l2 and should not directly link nodes from l1
to l2 (i.e. the nodes inserted into l1 should be copies of the nodes from l2.)

3. Write a function to reverse the nodes in a linked list. Your function should have time
complexity O(n), where n is the length of the list. You should create no new nodes.

4. Print all the elements at the index of multiples of k with the first element assumed to
have an index of 0. Do this for a single pass of the linked list.
Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> NULL

5. Extend the above solution assuming that the list is circular and the N-th index is the
same as 0-th index. You may need multiple passes. However, every number should be
printed only once during its first selection.
Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> 18 -> 20 -> 15 -> 19 -> NULL

6. Delete duplicate elements from a given linked list. Retain the earliest entries.
Input:
20 -> 18 -> 15 -> 20 -> 6 -> 18 -> 5 -> 3 -> NULL
Output:
20 -> 18 -> 15 -> 6 -> 5 -> 3 -> NULL

7. Reverse a linked list in groups of given size.


Input:
k=4
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 12 -> 7 -> NULL
Output:
4 -> 3 -> 2 -> 1 -> 8 -> 7 -> 6 -> 5 -> 7 -> 12 -> NULL

8. Remove in a linked list all the nodes that have a greater value to their right.
Input:
10 -> 12 -> 15 -> 20 -> 5 -> 16 -> 25 -> 8 -> NULL
10 -> 12 -> 15 -> 20 -> 25 -> 26 -> 30 -> 40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
Output:
20 -> 25 -> 8 -> NULL
40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL

9. Remove alternate nodes from a given linked list.


Input:
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
Output:
20 -> 15 -> 8 -> 5 -> NULL

10. Perform pair-wise swapping of nodes of a given linked list.


Input:
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> 7 -> NULL
Output:
18 -> 20 -> 10 -> 15 -> 6 -> 8 -> 3 -> 5 -> 7 -> NULL

11. Compare the worst-case big-O time analysis for these two functions: The insertfunction
for the sequence that is implemented using a fixed-sized array, and the insert function
for the sequence that is implemented using a linked list.

12. Find the middle element of a given linked list. In case of tie print the second one.
Input:
5 -> 7 -> NULL
5 -> 7 -> 17 -> NULL
5 -> 7 -> 17 -> 13 -> NULL
5 -> 7 -> 17 -> 13 -> 11 -> NULL
Output:
7
7
17
17

13. Check whether a given singly linked list is palindrome or not.


Input:
a -> b -> NULL
a -> b -> a -> NULL
s -> a -> g -> a -> r -> NULL
r -> a -> d -> a -> r -> NULL
Output:
not palindrome
palindrome
not palindrome
palindrome

14. Calculate the frequency of occurrence of each element in a given linked list in the
same order they appear. Avoid printing multiple entries.
Input:
20 -> 18 -> 15 -> 20 -> 6 -> 18 -> 5 -> 18 -> NULL
Output:
Freq(20) = 2
Freq(18) = 3
Freq(15) = 1
Freq(6) = 1
Freq(5) = 1

15. Given a pair linked lists, insert nodes of second linked list into the first linked list at
alternate positions. Assume that the first linked list has at least as many elements as the
second.
Input:
1 -> 2 -> 3 -> NULL
4 -> 5 -> NULL
Output:
1 -> 4-> 2 -> 5 -> 3 -> NULL

16. Write a C program to find a pair in a singly linked list whose sum is equal to a given
value.
Test Data and Expected Output:
Original singly linked list:
1234567
Find a pair whose sum is equal to 4:
(1,3)
Find a pair whose sum is equal to 11:
(4,7) (5,6)
Find a pair whose sum is equal to 5:
(1,4) (2,3)
Find a pair whose sum is equal to 14:
Pair not found.

17. Write a C program to reverse alternate k nodes of a given singly linked list.
Test Data and Expected Output :
Original List: 1 2 3 4 5 6 7 8
Reverse alternate k (k=2) nodes of the said singly linked list:
21346578
Reverse alternate k (k=3) nodes of the said singly linked list:
31246587
Reverse alternate k (k=4) nodes of the said singly linked list:
42136587

18. Write a program in C to insert a new node in the middle of a doubly linked list.
Test Data and Expected Output :
Doubly Linked List : Insert new node at the middle in a doubly linked list :
Input the number of nodes (3 or more): 3
Input data for node 1 : 2
Input data for node 2 : 4
Input data for node 3 : 5
Data entered in the list are :
node 1 : 2
node 2 : 4
node 3 : 5
Input the position ( 2 to 2 ) to insert a new node : 2
Input data for the position 2 : 3
After insertion the new list are :
node 1 : 2
node 2 : 3
node 3 : 4
node 4 : 5

19. Write a program in C to delete a node from the last node of a doubly linked list.
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list are :
node 1 : 1
node 2 : 2
node 3 : 3
After deletion the new list are :
node 1 : 1
node 2 : 2

20. Write a program in C to find the maximum value in a doubly linked list.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 9
Input data for node 3 : 1
Expected Output :
Data entered in the list are :
node 1 : 5
node 2 : 9
node 3 : 1
The Maximum Value in the Linked List : 9
Click me to see the solution

21. Write a program in C to insert a node at any position in a circular linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
Data 1 = 2
Data 2 = 5
Data 3 = 8
Input the position to insert a new node : 3
Input data for the position 3 : 7
After insertion the new list are :
Data 1 = 2
Data 2 = 5
Data 3 = 7
Data 4 = 8

22. Write a program in C to delete a node from the middle of a circular linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8

Data entered in the list are :


Data 1 = 2
Data 2 = 5
Data 3 = 8
Input the position to delete the node : 3
The deleted node is : 8
After deletion the new list are :
Data 1 = 2
Data 2 = 5

23. Write a program in C to search an element in a circular linked list.


Test Data and Expected Output :
Circular Linked List : Search an element in a circular linked list :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 9
Data entered in the list are :
Data 1 = 2
Data 2 = 5
Data 3 = 9
Input the element you want to find: 5
Element found at node 2

24. Write a C program to sort a given linked list by bubble sort.


Test Data and Expected Output: 5
15
33
49
6
65
Input number of elements in the linked list? Input the elements in the linked list:
Sorted order is:
6 15 33 49 65
25. Write a C program to convert a Doubly Linked list into a string.
Test Data and Expected Output :
Input the number of nodes: 4
Input data for node 1 : 10
Input data for node 2 : 11
Input data for node 3 : 12
Input data for node 4 : 13
The doubly linked list in string format: 10 11 12 13

26. Write a C program to convert a doubly linked list into an array and return it.
Test Data and Expected Output :
Input the number of nodes: 4
Input data for node 1 : 10
Input data for node 2 : 11
Input data for node 3 : 12
Input data for node 4 : 13
Doubly linked list in array format:
10 11 12 13

You might also like