w3resource

C++ Linked List: Exercises, Practice, Solution

C++ Linked List [21 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 C++ program to create and display a Singly Linked List.
Test Data:
The list contains the data entered:
11 9 7 5 3 1
Click me to see the sample solution

2. Write a C++ program to create a singly linked list of n nodes and display it in reverse order.
Test Data:
Original Linked list:
11 9 7 5 3 1
Reverse Linked list:
1 3 5 7 9 11
Click me to see the sample solution

3. Write a C++ program to create a singly linked list of n nodes and count the number of nodes.
Test Data:
Original Linked list:
13 11 9 7 5 3 1
Number of nodes in the said Linked list:
7
Click me to see the sample solution

4. Write a C++ program to insert a new node at the beginning of a Singly Linked List.
Test Data:
Original Linked list:
13 11 9 7 5 3 1
Insert a new node at the beginning of a Singly Linked List:
0 13 11 9 7 5 3 1
Click me to see the sample solution

5. Write a C++ program to insert a new node at the end of a Singly Linked List.
Test Data:
Original Linked list:
13 11 9 7 5 3 1
Insert a new node at the end of a Singly Linked List:
13 11 9 7 5 3 1 0
Click me to see the sample solution

6. Write a C++ program to find the middle element of a given Linked List.
Test Data:
Original list:
7 5 3 1
Middle element of the said list:
3
Original list:
9 7 5 3 1
Middle element of the said list:
5
Click me to see the sample solution

7. Write a C++ program to insert a new node at the middle of a given Singly Linked List.
Test Data:
Original list:
7 5 3 1
Singly Linked List: after insert 9 in the middle of the said list-
7 5 9 3 1
Singly Linked List: after insert 11 in the middle of the said list-
7 5 9 11 3 1
Singly Linked List: after insert 13 in the middle of the said list-
7 5 9 13 11 3 1
Click me to see the sample solution

8. Write a C++ program to get Nth node in a given Singly Linked List.
Test Data:
Original list:
7 5 3 1
Position: 1
Value: 7
Position: 2
Value: 5
Position: 3
Value: 3
Position: 4
Value: 1
Click me to see the sample solution

9. Write a C++ program to insert a new node at any position of a Singly Linked List.
Test Data:
Original list:
7 5 3 1
Position: 1, Value: 12
Updated list:
12 7 5 3 1
Position: 4, Value: 14
Updated list:
12 7 5 14 3 1
Position: 7, Value: 18
Updated list:
12 7 5 14 3 1 18
Click me to see the sample solution

10. Write a C++ program to delete first node of a given Singly Linked List.
Test Data:
Original Linked list:
13 11 9 7 5 3 1
Delete first node of Singly Linked List:
11 9 7 5 3 1
Click me to see the sample solution

11. Write a C++ program to delete a node from the middle of Singly Linked List.
Test Data:
Original list:
9 7 5 3 1
After removing the middle element of the said list:
9 7 3 1
After removing the middle element of the said list:
9 7 1
After removing the middle element of the said list:
9 1
After removing the middle element of the said list:
9
Click me to see the sample solution

12. Write a C++ program to delete the last node of a Singly Linked List.
Test Data:
Original list:
7 5 3 1
Remove the last node of the said list:
Updated list:
7 5 3
Again remove the last node of the said list:
Updated list:
7 5
Click me to see the sample solution

13. Write a C++ program to delete the nth node of a Singly Linked List from the end.
Test Data:
Original list:
7 5 3 1
Remove the 2nd node from the end of the said list:
Updated list:
7 5 1
Remove the 3rd node from the end of the said list:
Updated list:
5 1
Click me to see the sample solution

14. Write a C++ program to find the kth node of a linked list by starting at the middle and moving towards the head.
Test Data:
Original list:
9 7 5 3 1
kth node of a linked list by starting at
the middle and moving towards the head:
Position = 2
Value = 9
Click me to see the sample solution

15. Write a C++ program to create and display a doubly linked list.
Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Click me to see the sample solution

16. Write a C++ program to create a doubly linked list of n nodes and display it in reverse order.
Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Reverse Doubly linked list:
Traversal in Forward direction:
Red Green White Orange
Traversal in Reverse direction:
Orange White Green Red
Click me to see the sample solution

17. Write a program in C++ to create a doubly linked list of n nodes and count the number of nodes.
Test Data:
Doubly linked list is as follows:
Traversal in Forward direction:
Orange White Green Red
Traversal in Reverse direction:
Red Green White Orange
Total number of nodes = 4
Click me to see the sample solution

18. Write a C++ program to insert a new node at the beginning of a Doubly Linked List.
Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the beginning of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Pink Orange White Green Red
Traversal in Reverse direction: Red Green White Orange Pink
Click me to see the sample solution

19. Write a C++ program to insert a new node at the end of a Doubly Linked List.
Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Orange White Green Red Pink
Traversal in Reverse direction: Pink Red Green White Orange
Click me to see the sample solution

20. Write a C++ program to find the middle element of a given Doubly Linked List.
Test Data:
Doubly linked list is as follows:
------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
The middle element is: Green
Insert a new node at the end of a Doubly Linked List:
-------------------------------------------------------------
Traversal in Forward direction: Pink Orange White Green Red
Traversal in Reverse direction: Red Green White Orange Pink
The middle element is: White
Click me to see the sample solution

21. Write a C++ program to insert a new node at the middle of a given Doubly Linked List.
Test Data:
Doubly linked list is as follows:
---------------------------------
Traversal in Forward direction: Orange White Green Red
Traversal in Reverse direction: Red Green White Orange
Insert a new node at the middle position of the said Doubly linked list:
-----------------------------------------------------------------------
Traversal in Forward direction: Orange White Pink Green Red
Traversal in Reverse direction: Red Green Pink White Orange
Insert another new node at the middle position of the said Doubly linked list:
-----------------------------------------------------------------------------
Traversal in Forward direction: Orange White Pink Black Green Red
Traversal in Reverse direction: Red Green Black Pink White Orange
Click me to see the sample solution

CPP 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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/linked_list/index.php