You are on page 1of 1

Complete Linked List program in C++ last=x;

#include <iostream.h> }
#include <conio.h> }
#include <stdlib.h> void display()
struct node {
{ cout<<"Data in all the nodes"<<endl;
int data; last=first;
node *link; while(last!=NULL)
}*first,*x,*last,*temp1,*temp2,*temp3,*temp4; {
int i,limit,num,choice,ans,del_pos,ins_pos,ins_data; cout<<last->data<<endl;
void create(); last=last->link;
void display(); }
void del(); }
void ins(); void del()
void main() {
{ temp1=first;
clrscr(); cout<<"Enter the position of the noed to delete"<<endl;
do cin>>del_pos;
{ if(del_pos==1)
cout<<"1.Create Linked List"<<endl; {
cout<<"2.Display Nodes"<<endl; first=temp1->link;
cout<<"3.Delete Node"<<endl; cout<<"Node deleted:"<<temp1->data<<endl;
cout<<"4.Insert Node"<<endl; free (temp1);
cout<<"Enter Your Choice(Press 1,2,3 or 4)"<<endl; return;
cin>>choice; }
switch(choice) for(i=1;i<del_pos-1;i++)
{ {
case 1: temp1=temp1->link;
create(); }
break; temp2=temp1->link;
case 2: temp1->link=temp2->link;
display(); cout<<"Node deleted:"<<temp2->data<<endl;
break; free(temp2);
case 3: }
del(); void ins()
break; {
case 4: temp3=new node;
ins(); cout<<"Enter the position of the node to insert"<<endl;
break; cin>>ins_pos;
default: cout<<"Enter the data to be inserted at the position"<<endl;
cout<<"Wrong choice value"<<endl; cin>>ins_data;
} temp3->data=ins_data;
cout<<"Do You Wish to continue?(press 1 for Yes)"<<endl; if(ins_pos==1)
cin>>ans; {
}while(ans==1);
getch(); temp3->link=first;
} first=temp3;
void create() return;
{ }
first=new node(); temp4=first;
cout<<"Enter the data for the first node"<<endl; for(i=1;i<ins_pos-1;i++)
cin>>num; {
first->data=num; temp4=temp4->link;
first->link=NULL; }
last=first; temp3->link=temp4->link;
cout<<"Enter the number of nodes to be created after first temp4->link=temp3;
node"<<endl;
cin>>limit; }
for(i=1;i<=limit;i++)
{
x=new node;
cout<<"Enter data for node "<<i+1<<endl;
cin>>num;
x->data=num;
x->link=NULL;
last->link=x;

You might also like