1.Insert at beg 2.Insert at end
3.Insert at specific location
Source Code:-
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{int info;
struct node *next;
}*start, *temp;
void insert_beg(int);
void insert_end(int);
void insert_spe(int,int);
void display();
void main()
{clrscr();
start=NULL;
int item,choice,location,element,position;
cout<<endl<<"Insert Element in Link List by-Tarun Rawat\n";
again:
cout<<"\n1.Insert at Beg of the linked list .\n";
cout<<"2.Insert at End of the linked list\n";
cout<<"3.Insert at specific location\n4.Display Linked list\n5.Exit Program\n";
cout<<"Enter Choice : ";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter item to insert : ";
cin>>item;
insert_beg(item);
goto again;
case 2:cout<<"Enter item to insert : ";
cin>>item;
insert_end(item);
goto again;
case 3:cout<<"Enter location to insert : ";
cin>>location;
cout<<"Enter item to insert : ";
cin>>item;
insert_spe(item,location);
goto again;
case 4:cout<<"\nInserted item = ";
display();
goto again;
case 5:cout<<"\nTHANK YOU";
default:break;
}
getch();
}
void insert_beg(int item)
{ temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=start;
start=temp;
}
void insert_end(int item)
{ temp=(node*)malloc(sizeof(node));
temp->info=item;
temp->next=NULL;
node* current=start;
while(current->next!=NULL)
{current=current->next;
}
current->next=temp;
}
void insert_spe(int item,int location)
{ temp=(node*)malloc(sizeof(node));
temp->info=item;
node* current=start;
int count=1;
while (count <location-1)
{current=current->next;
count=count+1;
}
temp->next=current->next;
current->next=temp;
}
void display()
{ temp=start;
while(temp!=NULL)
{ cout<<temp->info<<" ";
temp=temp->next;
}
cout<<"\n";
}
No comments:
Post a Comment