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

1. Write a C++ program to create and display a Singly Linked List. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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.



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

What is the usefulness of `enable_shared_from_this?

It enables you to get a valid shared_ptr instance to this, when all you have is this. Without it, you would have no way of getting a shared_ptr to this, unless you already had one as a member.

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr p(new Y);
    shared_ptr q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}

The method f() returns a valid shared_ptr, even though it had no member instance. Note that you cannot simply do this:

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_ptr(this);
    }
}

The shared pointer that this returned will have a different reference count from the "proper" one, and one of them will end up losing and holding a dangling reference when the object is deleted.

Ref : https://bit.ly/3pwVzzz