C Programming Exercises, Practice, Solution : Linked List
C Singly Linked List [42 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a program in C to create and display a Singly Linked List.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7
Expected Output :
Data entered in the list : Data = 5 Data = 6 Data = 7
2. Write a program in C to create a singly linked list of n nodes and display it in reverse order.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7
Expected Output :
Data entered in the list are : Data = 5 Data = 6 Data = 7 The list in reverse are : Data = 7 Data = 6 Data = 5
3. Write a program in C to create a singly linked list of n nodes and count the number of nodes.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7
Expected Output :
Data entered in the list are : Data = 5 Data = 6 Data = 7 Total number of nodes = 3
4. Write a program in C to insert a new node at the beginning of a Singly Linked List.
Test Data and Expected Output :
Input the number of nodes : 3 Input data for node 1 : 5 Input data for node 2 : 6 Input data for node 3 : 7 Data entered in the list are : Data = 5 Data = 6 Data = 7 Input data to insert at the beginning of the list : 4 Data after inserted in the list are : Data = 4 Data = 5 Data = 6 Data = 7
5. Write a program in C to insert a new node at the end of a Singly Linked List.
Test Data and Expected Output :
Input the number of nodes : 3 Input data for node 1 : 5 Input data for node 2 : 6 Input data for node 3 : 7 Data entered in the list are : Data = 5 Data = 6 Data = 7 Input data to insert at the end of the list : 8 Data, after inserted in the list are : Data = 5 Data = 6 Data = 7 Data = 8
6. Write a program in C to insert a node in the middle of a Singly Linked List.
Test Data and Expected Output :
Input the number of nodes (3 or more) : 4 Input data for node 1 : 1 Input data for node 2 : 2 Input data for node 3 : 3 Input data for node 4 : 4 Data entered in the list are : Data = 1 Data = 2 Data = 3 Data = 4 Input data to insert in the middle of the list : 5 Input the position to insert new node : 3 Insertion completed successfully. The new list are : Data = 1 Data = 2 Data = 5 Data = 3 Data = 4
7. Write a program in C to delete the first node of a Singly Linked List.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 3
Input data for node 3 : 4
Expected Output :
Data entered in the list are : Data = 2 Data = 3 Data = 4 Data of node 1 which is being deleted is : 2 Data, after deletion of first node : Data = 3 Data = 4
8. Write a program in C to delete a node from the middle of a Singly 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 = 2 Data = 5 Data = 8 Input the position of node to delete : 2 Deletion completed successfully. The new list are : Data = 2 Data = 8
9. Write a program in C to delete the last node of a Singly Linked List.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Expected Output :
Data entered in the list are : Data = 1 Data = 2 Data = 3 The new list after deletion the last node are : Data = 1 Data = 2
10. Write a program in C to search for an existing element in a singly 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 = 2 Data = 5 Data = 8 Input the element to be searched : 5 Element found at node 2
11. Write a C program that converts a singly linked list into a string and returns it.
Test Data and Expected Output :
Linked List: Convert a Singly Linked list into a string ------------------------------------------------------------- Input the number of nodes: 3 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Return data entered in the list as a string: The linked list: 10 20 30
12. Write a C program that converts a singly linked list into an array and returns it.
Test Data and Expected Output :
Linked List: Convert a Singly Linked list into a string ------------------------------------------------------------- Input the number of nodes: 3 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Return data entered in the list as a string: The linked list: 10 20 30
13. Write a C program to merge two sorted singly linked lists into a single sorted linked list.
Test Data and Expected Output :
Two sorted singly linked lists: 1 3 5 7 2 4 6 After merging the said two sorted lists: 1 2 3 4 5 6 7
14. Write a C program to detect and remove a loop in a singly linked list.
Test Data and Expected Output :
Original singly linked list: 1 2 3 4 5 Create the loop: Following statement display the loop: displayList(head); After removing the said loop: 1 2 3 4 5
15. Write a C program to check if a singly linked list is a palindrome or not.
Test Data and Expected Output :
Original Singly List: 1 2 3 4 5 Linked list is not a palindrome. Original Singly List: 1 2 2 1 Linked list is a palindrome. Original Singly List: MADAM Linked list is a palindrome.
16. Write a C program to remove duplicates from a single unsorted linked list.
Test Data and Expected Output :
Original Singly List: 1 2 3 3 4 After removing duplicate elements from the said singly list: 1 2 3 4 Original Singly List: 1 2 3 3 4 4 After removing duplicate elements from the said singly list: 1 2 3 4
17. Write a C program to sort a singly linked list using merge sort.
Test Data and Expected Output :
Sort the said singly linked list using merge sort: 2 3 1 7 5 After sorting the said list: 1 2 3 5 7
18. Write a C program to create a copy of a singly linked list with random pointers.
Test Data and Expected Output :
Original singly list: 1 2 3 5 7 After setting the random pointers: Data: 1, Random: 3 Data: 2, Random: 5 Data: 3, Random: 7 Data: 5, Random: 1 Data: 7, Random: 3
19. Write a C program to find the intersection of two singly linked lists.
Test Data and Expected Output :
Original lists: 1 2 3 4 5 3 4 Intersection found at 3. Original lists: 1 2 3 4 5 3 4 Intersection not found.
20. Write a C program to get the n number of nodes from the end of a singly linked list.
Test Data and Expected Output :
Original list: 1 3 5 11 Last 2 nodes from the end of the said singly list: 5 11 Last 3 nodes from the end of the said singly list: 3 5 11 Last 4 nodes from the end of the said singly list: 1 3 5 11 Last 1 node from the end of the said singly list: 11 Last 5 node from the end of the said singly list:
21. Write a C program to partition a singly linked list based on a specific value.
Test Data and Expected Output:
Original list: 3 5 7 5 9 2 1 Linked List after partition around 5: 3 2 1 5 7 5 9
22. Write a C program that takes two linked lists of numbers. Each node contains a single digit and returns the sum of those numbers of said linked lists as a linked list.
Test Data and Expected Output :
List-1 8 7 9 2 List-2 2 1 2 3 Adding said two lists: 0 9 1 6
23. Write a C program that rotates a singly linked list to the right by k places.
Test Data and Expected Output :
Original List: 1 3 4 7 9 Rotate the said singly linked list to the right by 1 places: 9 1 3 4 7 Rotate the said singly linked list to the right by 2 places: 4 7 9 1 3 Rotate the said singly linked list to the right by 4 places: 7 9 1 3 4
24. Write a C program to swap Kth node from the beginning with Kth node from the end in a singly linked list.
Test Data and Expected Output :
Original List: 1 3 4 7 9 Rotate the said singly linked list to the right by 1 places: 9 1 3 4 7 Rotate the said singly linked list to the right by 2 places: 4 7 9 1 3 Rotate the said singly linked list to the right by 4 places: 7 9 1 3 4
25. Write a C program that removes elements with odd indices from a singly linked list.
Test Data and Expected Output :
Original linked list: 7 6 5 4 3 2 1 Linked list after removing odd indices: 6 4 2
26. Write a C program that removes elements with even indices from a singly linked list.
Test Data and Expected Output :
Original linked list: 7 6 5 4 3 2 1 Linked list after removing even indices: 6 4 2
27. Write a C program to implement a binary tree using linked list representation.
Test Data and Expected Output :
Traversal of a binary tree: 40 20 50 10 30
28. Write a C program to remove the Nth node from the end of a singly linked list.
Test Data and Expected Output :
Original Singly List: 1 2 3 4 5 Remove 1st node from the end of a singly linked list: 1 2 3 4 Remove 3rd node from the end of a singly linked list: 1 3 4
29. Write a C program to merge k sorted linked lists into a single sorted linked list.
Test Data and Expected Output :
List-1: 10 20 50 List-2: 30 40 60 List-3: 10 70 100 After merging the said three sorted lists into one sorted list: 10 10 20 30 40 50 60 70 100
30. Write a C program to create and reorder a linked list placing all even-numbered nodes ahead of all odd-numbered nodes.
Test Data and Expected Output :
Original Singly List: 1 2 3 4 5 6 Reorder the said linked list placing all even-numbered nodes ahead of all odd-numbered nodes: 1 3 5 2 4 6
31. Write a C program to reverse a singly linked list in pairs.
Test Data and Expected Output :
Original List: 1 2 3 4 5 6 Reverse a singly linked list in pairs: 2 1 4 3 6 5 Original List: 1 2 3 4 5 Reverse a singly linked list in pairs: 2 1 4 3 5
32. Write a C program to split a singly linked list into two halves.
Test Data and Expected Output :
Original List: 1 2 3 4 5 Split the said singly linked list into halves: First half: 1 2 3 Second half: 4 5 Original List: 1 2 3 4 5 6 Split the said singly linked list into halves: First half: 1 2 3 Second half: 4 5 6
33. Write a C program to delete alternate nodes of a singly linked list.
Test Data and Expected Output :
Original List: 1 2 3 4 5 6 7 Delete alternate nodes of the said singly linked list: 1 3 5 7
34. Write a C program to to merge alternate nodes of two singly linked lists.
Test Data and Expected Output :
First linked list: 9 7 5 3 1 Second linked list: 10 8 6 4 2 Merged linked list: 9 10 7 8 5 6 3 4 1 2
35. Write a C program to remove duplicates from a sorted singly linked list.
Test Data and Expected Output :
Original sorted singly linked list: 1 2 3 3 5 6 6 After removing duplicates from the said sorted linked list: 1 2 3 5 6
36. Write a C program to reverse a singly linked list starting at the first position in blocks of size k.
Test Data and Expected Output :
Given linked list: 1 2 3 4 5 6 7 8 Reverse the first 3 nodes of the said Linked list: 3 2 1 6 5 4 8 7 Reverse the first 5 nodes of the said Linked list: 5 6 1 2 3 7 8 4
37. Write a C program to remove all elements from a singly linked list that are greater than a given value x.
Test Data and Expected Output :
Given linked list: 1 2 3 4 5 6 7 8 Reverse the first 3 nodes of the said Linked list: 3 2 1 6 5 4 8 7 Reverse the first 5 nodes of the said Linked list: 5 6 1 2 3 7 8 4
38. 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: 1 2 3 4 5 6 7 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.
39. Write a C program to interleave elements of two singly linked lists alternatively.
Test Data and Expected Output :
Original Lists: List1: 1 3 5 7 List2: 2 4 6 8 After interleaving the two linked lists alternatively: List1: 1 2 3 4 5 6 7 8 List2: 2 3 4 5 6 7 8
40. Write a C program to swap every two adjacent nodes of a given singly linked list.
Test Data and Expected Output :
Original List: 1 2 3 4 5 Updated List after swapping every two adjacent nodes: 2 1 4 3 5
41. 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: 2 1 3 4 6 5 7 8 Reverse alternate k (k=3) nodes of the said singly linked list: 3 1 2 4 6 5 8 7 Reverse alternate k (k=4) nodes of the said singly linked list: 4 2 1 3 6 5 8 7
42. Write a C program to find the point at which two singly linked lists intersect.
Test Data and Expected Output :
List-1: 1 2 7 List-2: 3 4 5 7 Intersection found at node with data: 7 List-3: 1 2 5 List-4: 3 4 5 7 No intersection found.
C Doubly Linked List [22 exercises with solution]
1. Write a program in C to create and display a doubly linked list.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Expected Output :
Data entered on the list are : node 1 : 2 node 2 : 5 node 3 : 8
2. Write a program in C to create a doubly linked list and display it in reverse order.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Expected Output :
Data in reverse order are : Data in node 1 : 8 Data in node 2 : 5 Data in node 3 : 2
3. Write a program in C to insert a node at the beginning of a doubly 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 : node 1 : 2 node 2 : 5 node 3 : 8 Input data for the first node : 1 After insertion the new list are : node 1 : 1 node 2 : 2 node 3 : 5 node 4 : 8
4. Write a program in C to insert a new node at the end of a doubly 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 : node 1 : 2 node 2 : 5 node 3 : 8 Input data for the last node : 9 After insertion the new list are : node 1 : 2 node 2 : 5 node 3 : 8 node 4 : 9
5. Write a program in C to insert a new node at any position in a doubly linked list.
Test Data and Expected Output :
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
6. 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
7. Write a program in C to delete a node from the beginning 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 : 2 node 2 : 3
8. 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
9. Write a program in C to delete a node from any position in a doubly linked list.
Test Data and Expected Output :
Doubly Linked List : Delete node from any position of a doubly linked list : ---------------------------------------------------------------------------------- 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 Input the position ( 1 to 3 ) to delete a node : 3 After deletion the new list are : node 1 : 1 node 2 : 2
10. Write a program in C to delete a node from the middle 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 Input the position ( 1 to 3 ) to delete a node : 2 After deletion the new list are : node 1 : 1 node 2 : 3
11. 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
12. Write a program in C to create and display a circular linked list.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Expected Output :
Data entered in the list are : Data 1 = 2 Data 2 = 5 Data 3 = 8
13. Write a program in C to insert a node at the beginning 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 data to be inserted at the beginning : 1 After insertion the new list are : Data 1 = 1 Data 2 = 2 Data 3 = 5 Data 4 = 8
14. Write a program in C to insert a node at the end 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 data to be inserted : 9 After insertion the new list are : Data 1 = 2 Data 2 = 5 Data 3 = 8 Data 4 = 9
15. 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
16. Write a program in C to delete a node from the beginning of a circular linked list.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Expected Output :
Data entered in the list are : Data 1 = 2 Data 2 = 5 Data 3 = 8 The deleted node is -> 2 After deletion the new list are : Data 1 = 5 Data 2 = 8
17. 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
18. Write a program in C to delete the node at the end 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 The deleted node is : 8 After deletion the new list are : Data 1 = 2 Data 2 = 5
19. 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
20. 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
21. 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
22. 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
C Programming Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics