#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node* link;
};
node *start=NULL,*x,*y,*z;
void create()
{
if(start==NULL)
{
start=new node;
cout<<"\nEnter data: ";
cin>>start->data;
start->link=NULL;
x=start;
}
else
{
y=new node;
cout<<"\nEnter data: ";
cin>>y->data;
y->link=NULL;
x->link=y;
x=y;
}
}
void begin()
{
if(start==NULL)
cout<<"\nNo data present.\n";
else
{
z=start;
start=start->link;
z->link=NULL;
delete z;
}
}
void end() // ERROR: cannot create after deletion at end.
{
if(start==NULL)
cout<<"\nNo data present.\n"<<endl;
else
{
z=start;
y=z->link;
while(y->link!=NULL)
{
z=z->link;
y=y->link;
}
z->link=NULL;
delete y;
}
}
void mid()
{
int val;
if(start==NULL)
cout<<"\nNo Data present.\n";
else
{
cout<<"\nEnter value which is to be deleted: ";
cin>>val;
z=start;
y=z->link;
if(z->data==val)
{
start=start->link;
z->link=NULL;
delete z;
}
else
{
while(y!=NULL && y->data!=val)
{
y=y->link;
z=z->link;
}
if(y->data==val)
{
z->link=y->link;
y->link=NULL;
delete y;
}
}
}
}
void display()
{
z=start;
cout<<"\nLink list as follows: ";
while(z!=NULL)
{
cout<<z->data<<" ";
z=z->link;
}
cout<<"\n\n";
}
void main()
{
clrscr();
int ch;
do
{
cout<<"1. Create a Link List."<<endl;
cout<<"2. Delete at begining."<<endl;
cout<<"3. Delete at end."<<endl;
cout<<"4. Delete in the middle."<<endl;
cout<<"5. Display Link List."<<endl;
cout<<"6. Exit."<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: {
create();
break;
}
case 2: {
begin();
break;
}
case 3: {
end();
break;
}
case 4: {
mid();
break;
}
case 5: {
display();
break;
}
case 6: {
break;
}
default:{
cout<<"\nWrong choice entered.\n";
}
}
cout<<endl;
}
while(ch!=6);
}