Menu Driven Program for Singly Linked List in C++

In this post, we are going to make a Menu Driven Program for Singly Linked List in C++ to perform the following operations:

  1. Traversal: Display all the values which are present in a singly linked list.
  2. Insertion: Inserting a new element in a singly linked list from rear end.
  3. Deletion: Removing element of a singly linked list from the beginning.
  4. Searching: Finding an element in a singly linked list

Menu Driven Program for Singly Linked List in C++

Here is a menu driven program to create insert, delete and display operations on singly linked list in c++.

#include<iostream>
using namespace std;
struct node{
int data;
node*next;
};
node *head,*newnode,*temp;
void traversal(struct node*ptr);
void insertion();
void deletion();
int search();
int main()
{
char ch;
int choice;
head = NULL;
do
{

cout<<"\n \t\t Menu Driven Program";
cout<<"\n 1. Traversal \n 2. Insertion \n 3. Deletion \n 4. Searching";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
        case 1: traversal(head);
                break;
        case 2: insertion();
                break;
        case 3: deletion();
                break;
        case 4: int pos;
                pos=search();
                if(pos==-1)
                cout<<"\n Element not found";
                else
                cout<<"\n Element is found at Position:"<<pos;
                break;
        default:cout<<"wrong choice";
}
        cout<<"\n Do you want to continue?";
        cout<<"(Press y/n)";
        cin>>ch;
        }while(ch=='y');

return 0;
}

void traversal(struct node*ptr)
{   while(ptr!=NULL)
    {
        cout<<ptr->data<<" ";
        ptr=ptr->next;
}
}

void insertion()
{
    newnode=new node();
    cout<<"\n Enter data:";
    cin>>newnode->data;
    newnode->next=NULL;
    if(head==NULL)
    head=temp=newnode;
    else
    {
        temp->next=newnode;
        temp=newnode;
    }
}
void deletion()
{
       if(head==NULL)
       cout<<"\n List is empty";
       temp=head;
       head=head->next;
       free(temp);
}
int search()
{       int element;
        cout<<"\n Enter element to be searched:";
        cin>>element;
        temp=head;
        int count=0;
        while(temp!=0)
        {
                count++;
                if(temp->data==element)
                return count;
                temp=temp->next;
        }
        return -1;

}

Output of Menu Driven Program for Singly Linked List in C++:

Menu Driven Program
1. Traversal
2. Insertion
3. Deletion
4. Searching
Enter your choice:2
Enter data:22 44 66
Do you want to continue?(Press y/n)y
 
 Menu Driven Program
1. Traversal
2. Insertion
3. Deletion
4. Searching
Enter your choice:3
Do you want to continue?(Press y/n)y
 
Menu Driven Program
1. Traversal
2. Insertion
3. Deletion
4. Searching
Enter your choice:4
Enter element to be searched:66
Element is found at Position:2
Do you want to continue?(Press y/n)n
 
Related Posts:

About Menu Driven Program for Singly Linked List in C++:

This post is all about Menu Driven Program for Singly Linked List in C++ to perform the operations of Traversal, Insertion, Deletion and Searching in a singly linked list. If you have any doubts related to this program, let me know in the comment section. Please subscribe to our News-letter.

Siddharth Jha
• Hackerrank 5* • Full Stack Developer • Masters in SEO

Related Posts

Post a Comment