Data Structures Programs Implementation Using C++

Data Structures Programs Implementation Using C++

In this article, let’s implement some of the important data structure programs using C++.

1. Program to insert a node at the beginning, at the end, and in the middle of the given linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

--INSERT NODE AT BEGINNING--

Input data to insert at begin: 5
New node is added at begin successfully!

Input data to insert at begin: 3
New node is added at begin successfully!

--INSERT NODE AT END--

Input data to insert at End: 10
New node is added at end successfully!

Input data to insert at End: 11
New node is added at end successfully!

--INSERT NODE AT MIDDLE--

Input data and position insert at Middle: 2 2
New node is added at given position successfully!

Input data and position insert at Middle: 4 4
New node is added at given position successfully!

--Single Linked List after insertion:
3 -> 2 -> 5 -> 4 -> 10 -> 11 ->

2. Program to delete a node at the beginning, at the end, and in the middle of the given linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Create a Linked List
2. Delete From Begin
3. Delete From End
4. Delete From Desired Location
5. Display Single Linked List
6. Exit
Enter your choice: 1
Enter an item to insert: 5

Enter your choice: 1
Enter an item to insert: 4

Enter your choice: 1
Enter an item to insert: 3

Enter your choice: 1
Enter an item to insert: 2

Enter your choice: 1
Enter an item to insert: 1

Enter your choice: 5
5 -> 4 -> 3 -> 2 -> 1

Enter your choice: 2
5 node is deleted!

Enter your choice: 3
1 node is deleted!

Enter your choice: 4
Enter location to delete: 2
3 node is deleted!

Enter your choice: 5
Given Linked List is:
4 -> 2 ->

Enter your choice: 6

3. Program to create a linked list of customer names and telephone numbers. (Using Menu Driven and include features of adding a new Customer and deleting an existing Customer.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Add New Customer
2. Delete Customer
3. Display all Customer
4. Exit
Enter your choice: 1
Enter customer name (Do not add spaces): Ajay
Enter customer telephone: 9876543210
New customer is added successfully!

Enter your choice: 1
Enter customer name (Do not add spaces): Kunal
Enter customer telephone: 7896541230
New customer is added successfully!

Enter your choice: 1
Enter customer name (Do not add spaces): Puja
Enter customer telephone: 7896412980
New customer is added successfully!

Enter your choice: 3
      NAME        TELEPHONE NO
      Ajay          9876543210
     Kunal          7896541230
      Puja          7896412980

Enter your choice: 2
Enter customer name to delete (Do not add spaces): Kunal

Kunal is deleted!

Enter your choice: 3
      NAME        TELEPHONE NO
      Ajay          9876543210
      Puja          7896412980

Enter your choice: 4

4. Program to reverse a linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Create Linked List
2. Reverse Linked List
3. Display Linked List
4. Exit
Enter your choice: 1
Enter an item: 4

Enter your choice: 1
Enter an item: 3

Enter your choice: 1
Enter an item: 2

Enter your choice: 1
Enter an item: 1

Enter your choice: 2
Old Linked list is:
4 -> 3 -> 2 -> 1 ->
Linked list after reversal:
1 -> 2 -> 3 -> 4 ->

5. Program to search a value in the given linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Insert at Linked List
2. Search item
3. Display Linked List
4. Exit
Enter your choice: 1
Enter an item to insert: 5
New item is added successfully!

Enter your choice: 1
Enter an item to insert: 3
New item is added successfully!

Enter your choice: 1
Enter an item to insert: 7
New item is added successfully!

Enter your choice: 3
Given Linked List is:
5 -> 3 -> 7 ->

Enter your choice: 2
Enter an item to search: 3
3 found at 2 position.

6. Program to insert a node at the beginning, at the end, or in the middle of a given doubly linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Insert at Begin of Double Linked List
2. Insert at End of Double Linked List
3. Insert at Desired Location of Double Linked List
4. Display Double Linked List
5. Exit

Enter your choice: 1
Enter an item to insert: 3

Enter your choice: 1
Enter an item to insert: 2

Enter your choice: 1
Enter an item to insert: 1

Enter your choice: 2
Enter an item to insert: 4

Enter your choice: 3
Enter an item to insert: 5 3

Enter your choice: 4
Given Double Linked List is:
1 <=> 2 <=> 5 <=> 3 <=> 4

7. Program to delete a node from the beginning, in the end, or in the middle of a given doubly linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Insert at Double Linked List
2. Delete at Begin of Double Linked List
3. Delete at End of Double Linked List
4. Delete at Desired Location of Double Linked List
5. Display Double Linked List
6. Exit
Enter your choice: 1
Enter an item to insert: 5

Enter your choice: 1
Enter an item to insert: 4

Enter your choice: 1
Enter an item to insert: 3

Enter your choice: 1
Enter an item to insert: 2

Enter your choice: 1
Enter an item to insert: 1

Enter your choice: 5
Given Double Linked List is:
1 <=> 2 <=> 3 <=> 4 <=> 5

Enter your choice: 2
1 node is deleted.

Enter your choice: 3
5 node is deleted.

Enter your choice: 4
Enter a location to delete: 2
3 node is deleted.

Given Double Linked List is:
2 <=> 4

8. Program to create, insert and delete a node in the Circular linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Inserting nodes in Circular Linked List:

Circular Linked list after insertion:
15 -> 85 -> 94 -> 63

Deleting last 2 nodes from Circular Linked List:

Circular Linked list after deletion:
15 -> 63

9. Program to push and pop an element into/from a stack implemented using a linked list.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter element to push: 5

Enter your choice: 1
Enter element to push: 4

Enter your choice: 1
Enter element to push: 3

Enter your choice: 2
Element popped from stack: 3
Remaining stack elements are:
4
5

10. Program to push and pop an element into/from a stack implemented using Array.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter an item to Push: 4

Enter your choice: 1
Enter an item to Push: 3

Enter your choice: 1
Enter an item to Push: 2

Enter your choice: 1
Enter an item to Push: 1

Enter your choice: 2
Item Popped: 1

Enter your choice: 3
Item Popped: 1

Enter your choice: 3
Stack elements:
2
3
4

11. Program to evaluate postfix expression.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter an infix expression: 5+4*(3-6)/2

Converted postfix expression: 5436-*2/+

12. Program to sort an array using quick sort.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter size of an array: 5
Enter 1 element: 5
Enter 2 element: 4
Enter 3 element: 3
Enter 4 element: 2
Enter 5 element: 1

Array after quick sort:
1 2 3 4 5

13. Program to solve Towers of Hanoi problems using recursion.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter number of disks: 4

Move disk l from peg A to peg C
Move disk 2 from peg A to peg B
Move disk l from peg C to peg B
Move disk 3 from peg A to peg C
Move disk l from peg B to peg A
Move disk 2 from peg B to peg C
Move disk l from peg A to peg C
Move disk 4 from peg A to peg B
Move disk l from peg C to peg B
Move disk 2 from peg C to peg A
Move disk l from peg B to peg A
Move disk 3 from peg C to peg B
Move disk l from peg A to peg C
Move disk 2 from peg A to peg B
Move disk l from peg C to peg B

14. Program to perform insertion and deletion operations in linear queues.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter an item: 1

Enter your choice: 1
Enter an item: 2

Enter your choice: 1
Enter an item: 3

Enter your choice: 1
Enter an item: 4

Enter your choice: 3
1 2 3 4

Enter your choice: 2
1 is deleted!

Enter your choice: 3
2 3 4

Enter your choice: 4

15. Program to perform insertion and deletion operations on circular queues.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 1
Enter the element to push: 10

Enter the choice: 1
Enter the element to push: 20

Enter the choice: 1
Enter the element to push: 30

Enter the choice: 2
10 is deleted from queue.

Enter the choice: 3
Items in Queue:
2 -> 20
3 -> 30

16. Program to sort an array using Insertion sort.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter size of array: 5
Enter elements of array: 50 44 30 21 10

After Insertion sort:
10 21 30 44 50

17. Program to sort an array using Selection sort.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter size of array: 5
Enter elements of array: 5 4 3 2 1

After Selection sort:
1 2 3 4 5

18. Program to insert an element in a binary search tree.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter key to insert (0 to exit): 5
BST After insertion:
        5

Enter key to insert (0 to exit): 4
BST After insertion:
        5
                4

Enter key to insert (0 to exit): 1
BST After insertion:

        5
                4
                        1

Enter key to insert (0 to exit): 2
BST After insertion:

        5
                4
                                2
                        1

Enter key to insert (0 to exit): 0

19. Program to traverse inorder of a binary tree.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter item to insert in BST(0 to exit): 7
Enter item to insert in BST(0 to exit): 3
Enter item to insert in BST(0 to exit): 4
Enter item to insert in BST(0 to exit): 8
Enter item to insert in BST(0 to exit): 5
Enter item to insert in BST(0 to exit): 2
Enter item to insert in BST(0 to exit): 0

In-Order traversal of BST:
2 3 4 5 7 8

20. Program to traverse preorder of a binary tree.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter item to insert in BST(0 to exit): 7
Enter item to insert in BST(0 to exit): 3
Enter item to insert in BST(0 to exit): 4
Enter item to insert in BST(0 to exit): 8
Enter item to insert in BST(0 to exit): 5
Enter item to insert in BST(0 to exit): 2
Enter item to insert in BST(0 to exit): 0

Pre-Order traversal of BST:
7 3 2 4 5 8

21. Program to traverse postorder of a binary tree.

Let’s implement the above program in C++.

When you execute the above program, the output will look like this as shown below:

Enter item to insert in BST(0 to exit): 7
Enter item to insert in BST(0 to exit): 3
Enter item to insert in BST(0 to exit): 4
Enter item to insert in BST(0 to exit): 8
Enter item to insert in BST(0 to exit): 5
Enter item to insert in BST(0 to exit): 2
Enter item to insert in BST(0 to exit): 0

PostOrder traversal of BST:
2 5 4 3 8 7

Also, check:

C Programs for Practice: Learn Simple C Programs for Beginners
Simple C Programs Based on Discrete Mathematics

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top