#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
void main()
{
clrscr();
int i,ch,a,b,c;
ch=1;
head=NULL;
while( ch != 0)
{
cout<<"\nMENU\n";
cout<<"\t1.ADD A NODE\n";
cout<<"\t2.DISPLAY THE NODES\n";
cout<<"\t3.DELETION\n";
cout<<"\t0.QUIT\n";
cin>>ch;switch(ch)
{
case 1:
head=addnode(head);break;
case 2:
shownodes(head);break;
case 3:
head=delete_node(head);break;
case 0:
ch=0;break;
default:
cout<<"INVALID CHOICE ?? \n";break;
}
}
}
node * addnode(node *f)
{
node *n;
int ch;
n=new node;
cout<<"ENTER A ELEMENT TO BE INSERTED :";
cin>>n->data;
n->next=NULL;
if(f==NULL)
{f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
else{
cout<<"WHERE YOU WANT TO INSERT :\n";
cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
cin>>ch;
switch(ch)
{
case 1:n->next=f;f=n;cout<<"SUCCESS\n";break;
case 2: int pos;node * temp;temp=f;
cout<<"ENTER THE POSITION :\n";
cin>>pos;
for(int k=1;k<pos-1;k++)
{if(temp->next == NULL )
{cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
return f;}temp=temp->next;}
n->next=temp->next;temp->next=n;
cout<<"SUCCESS..\n";break;
case 3: temp=f;
while(temp->next != NULL)
{temp=temp->next;}
temp->next=n;cout<<"SUCCESS \n";break;
default:cout<<"INVALID ??\n";break;}}
return f;
}
void shownodes(node *f)
{
node *temp=f;
if ( temp == NULL ){
cout<<endl<<"EMPTY LIST ..\n";
return;}
else{
cout<<"HEAD";
while(temp!=NULL)
{cout<<" -> ";
cout<<temp->data;temp=temp->next;}
cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
node *temp1,*temp2;int ch;
temp1=temp2=f;
if( temp1 == NULL ){
cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
else{
cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
cin>>ch;
switch(ch){
case 1:f=f->next;cout<<"SUCCESS\n";break;
case 2: int pos;node * temp;temp=f;
cout<<"ENTER THE POSITION :\n";
cin>>pos;
for(int k=1;k<pos-1;k++)
{if(temp->next == NULL )
{cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
return f;}temp=temp->next;}
temp->next=temp->next->next;
cout<<"SUCCESS..\n";break;
case 3:while(temp1->next!=NULL){
temp2=temp1;temp1=temp1->next;}
temp2->next=NULL;
cout<<"SUCCESS \n";break;
default:cout<<"INVALID ??\n";break;}}
return f;
}
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
void main()
{
clrscr();
int i,ch,a,b,c;
ch=1;
head=NULL;
while( ch != 0)
{
cout<<"\nMENU\n";
cout<<"\t1.ADD A NODE\n";
cout<<"\t2.DISPLAY THE NODES\n";
cout<<"\t3.DELETION\n";
cout<<"\t0.QUIT\n";
cin>>ch;switch(ch)
{
case 1:
head=addnode(head);break;
case 2:
shownodes(head);break;
case 3:
head=delete_node(head);break;
case 0:
ch=0;break;
default:
cout<<"INVALID CHOICE ?? \n";break;
}
}
}
node * addnode(node *f)
{
node *n;
int ch;
n=new node;
cout<<"ENTER A ELEMENT TO BE INSERTED :";
cin>>n->data;
n->next=NULL;
if(f==NULL)
{f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
else{
cout<<"WHERE YOU WANT TO INSERT :\n";
cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
cin>>ch;
switch(ch)
{
case 1:n->next=f;f=n;cout<<"SUCCESS\n";break;
case 2: int pos;node * temp;temp=f;
cout<<"ENTER THE POSITION :\n";
cin>>pos;
for(int k=1;k<pos-1;k++)
{if(temp->next == NULL )
{cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
return f;}temp=temp->next;}
n->next=temp->next;temp->next=n;
cout<<"SUCCESS..\n";break;
case 3: temp=f;
while(temp->next != NULL)
{temp=temp->next;}
temp->next=n;cout<<"SUCCESS \n";break;
default:cout<<"INVALID ??\n";break;}}
return f;
}
void shownodes(node *f)
{
node *temp=f;
if ( temp == NULL ){
cout<<endl<<"EMPTY LIST ..\n";
return;}
else{
cout<<"HEAD";
while(temp!=NULL)
{cout<<" -> ";
cout<<temp->data;temp=temp->next;}
cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
node *temp1,*temp2;int ch;
temp1=temp2=f;
if( temp1 == NULL ){
cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
else{
cout<<"From\n\t1.BEG\n\t2.SPECIFIED LOCATION\n\t3.END\n";
cin>>ch;
switch(ch){
case 1:f=f->next;cout<<"SUCCESS\n";break;
case 2: int pos;node * temp;temp=f;
cout<<"ENTER THE POSITION :\n";
cin>>pos;
for(int k=1;k<pos-1;k++)
{if(temp->next == NULL )
{cout<<"NODES IN THE LIST IS LESS THAN THE POSITION YOU ENTERED..\n";
return f;}temp=temp->next;}
temp->next=temp->next->next;
cout<<"SUCCESS..\n";break;
case 3:while(temp1->next!=NULL){
temp2=temp1;temp1=temp1->next;}
temp2->next=NULL;
cout<<"SUCCESS \n";break;
default:cout<<"INVALID ??\n";break;}}
return f;
}