In this post, we are going to make a Menu Driven Program for Singly Linked List in C++ to perform the following operations:
- Traversal: Display all the values which are present in a singly linked list.
- Insertion: Insert a new element in a singly linked list from the rear end.
- Deletion: Removing elements of a singly linked list from the beginning.
- 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 in a 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 tt 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;
}
The 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
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
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 newsletter.
I’m extremely inspired along with your writing abiⅼities as
well as with the ѕtructure in your weblog. Is that
this a paiⅾ thеme or did yߋu custօmize it yourseⅼf?
Either way stay up the excellent quаlity writing, it is rare to seе
a grеat blog like this one today..
Helⅼo, just wanted to say, I enjoyed tһis post.
It was funny. Kеep on posting!