#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*start=NULL;
void create_list()
{
char ch;
do
{
if(start==NULL)
{ struct node *temp;
temp=new
node;
cout<<"enter
data:-";
cin>>temp->data;
temp->link=NULL;
start=temp;
}
else
{
struct
node *temp,*ptr;
temp=new
node;
cout<<"enter
data:-";
cin>>temp->data;
temp->link=NULL;
ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
cout<<"do
u want to continue(y/n):-";
cin>>ch;
}while(ch=='y');
}
void after_given(int n)
{
struct node
*temp,*ptr;
temp=new node;
cout<<"enter
data:-";
cin>>temp->data;
cout<<"insert
a element after given value"<<endl;
if(start->data==n)
{
temp->link=start->link;
start->link=temp;
}
else
{
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==n)
{
temp->link=ptr->link;
ptr->link=temp;
break;
}
else
{
ptr=ptr->link;
}
}//end
of while loop
}
}
void del_element(int n)
{
struct node
*ptr,*temp;
cout<<"
delete a element after given value"<<endl;
ptr=start;
while(ptr->data!=n)
{
ptr=ptr->link;
}
temp=ptr->link;
ptr->link=temp->link;
}
void display()
{
struct node *ptr;
ptr=start;
cout<<endl<<"linked
list is:-"<<endl;
while(ptr!=NULL)
{
cout<<"
"<<ptr->data<<" ";
ptr=ptr->link;
}
}
void main()
{
clrscr();
int choice;
int n,loc,c=0;
cout<<" press 1.
create linked list"<<endl;
cout<<" press 2.insert
the element after given value"<<endl;
cout<<" press 3.
display linked list"<<endl;
cout<<" press 4.delete
the element after given a specific value"<<endl;
cout<<" press 5.
EXIT"<<endl;
while(1)
{
cout<<"
enter your choice:-";
cin>>choice;
switch(choice)
{
case 1:
create_list();
break;
case 2:
cout<<"enter
value where you want to insert after:-";
cin>>n;
after_given(n);
break;
case 3:
display();
break;
case 4:
cout<<"enter
value that u want to delete next node :-";
cin>>n;
del_element(n);
break;
case 5:
exit(0);
break;
default:
cout<<"try
again";
break;
}//END OF SWITCH CASE
getch();
}//END OF WHILE
}//END OF MAIN
No comments:
Post a Comment