Program to implement singly linked list operations
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
PROGRAM
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class singlyll
{
node *start,*last,*head;
public:
void insertfirst();
void insertlast();
void insertloc();
void deletefirst();
void deletelast();
void deleteloc();
void display();
singlyll()
{
start=last=NULL;
}
};
void singlyll::insertfirst()
{
head=new node;
cout<<"Enter the number:";
cin>>head->data;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start=head;
}
}
void singlyll::insertlast()
{
head=new node;
cout<<"Enter the number:";
cin>>head->data;
head->next=NULL;
if(start==NULL)
start=last=head;
else
{
last->next=head;
last=head;
}
}
void singlyll::insertloc()
{
int pos;
node *pre,*temp;
cout<<"Enter position:";
cin>>pos;
if(pos==1)
insertfirst();
else
{
head=new node;
cout<<"Enter the number:";
cin>>head->data;
temp=start;
for(int i=1;i<pos;i++)
{
if(temp!=NULL)
{
pre=temp;
temp=temp->next;
}
else
{
head->next=NULL;
last->next=head;
last=head;
}
}
pre->next=head;
head->next=temp;
if(temp==NULL)
last=head;
}
}
void singlyll::deletefirst()
{
if(start==NULL)
cout<<"The list is empty\n";
else
{
node *temp;
temp=start;
cout<<"The deleted item is
"<<start->data;
if(start==last)
start=last=NULL;
else
start=start->next;
delete temp;
}
}
void singlyll::deletelast()
{
if(start==NULL)
cout<<"The list is empty\n";
else
{
node *temp=start;
cout<<"The deleted item is
"<<last->data;
if(start==last)
start=last=NULL;
else
{
while(temp->next!=last)
temp=temp->next;
temp->next=NULL;
delete last;
last=temp;
}
}
}
void singlyll::deleteloc()
{
if(start==NULL)
cout<<"The list is empty\n";
else
{
node *temp,*pre;
int pos;
cout<<"Enter position:\n";
cin>>pos;
if(pos==1)
deletefirst();
else
{
temp=start;
for(int i=1;i<pos;i++)
{
pre=temp;
temp=temp->next;
}
if(temp!=NULL)
{
if(temp==last)
last=pre;
pre->next=temp->next;
cout<<"The deleted item is
"<<temp->data;
delete temp;
}
else
cout<<"no data deleted\n";
}
}
}
void singlyll::display()
{
node *temp;
if(start==NULL)
cout<<"list is empty\n";
else
{
temp=start;
cout<<”The list elements are:\n”;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
}
int main()
{
singlyll l;
int ch;
char c;
do
{
cout<<"1.Insert first\n";
cout<<"2.Insert last\n";
cout<<"3.Insert at specified
location\n";
cout<<"4.Delete first\n";
cout<<"5.Delete last\n";
cout<<"6.Delete at specified
location\n";
cout<<"7.Display\n";
cout<<"Enter your choice:\n";
cin>>ch;
switch(ch)
{
case 1:l.insertfirst();
break;
case 2:l.insertlast();
break;
case 3:l.insertloc();
break;
case 4:l.deletefirst();
break;
case 5:l.deletelast();
break;
case 6:l.deleteloc();
break;
case 7:l.display();
break;
default:cout<<"invalid
option\n";
}
cout<<"\nDo you want to
continue,y/n:";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
OUTPUT
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
1
Enter
the number:5
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
2
Enter
the number:7
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
3
Enter
position:2
Enter
the number:6
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
7
The list elements are:
5 6
7
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
4
The
deleted item is 5
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
6
Enter
position:2
The
deleted item is 7
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
5
The
deleted item is 6
Do
you want to continue,y/n:y
1.Insert
first
2.Insert
last
3.Insert
at specified location
4.Delete
first
5.Delete
last
6.Delete
at specified location
7.Display
Enter
your choice:
7
list
is empty
Do
you want to continue,y/n:n
0 comments:
Post a Comment