Saturday, 12 October 2013

CIRCULAR LINK LIST

                   CIRCULAR  LINK LIST                
               INSERTION AND DELETION         

#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *link;
} *head;

void insbeg()
{
node *temp;
temp= new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
node *temp1;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
head=temp;
temp1->link=head;
}
}
}
void display()
{
node *temp;
temp=head;
if(head==0)
cout<<"\nNo linked list";
else
{
cout<<"\nLinked List is : ";
while(temp->link!=head)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<temp->data;
}
}
void insend()
{
node *temp,*temp1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data;
cout<<"Enter the data you want to insert: ";
cin>>data;
temp->data=data;
if(head==0)
{
temp->link=temp;
head=temp;
}
else
{
temp->link=head;
temp1=head;
while(temp1->link!=head)
{
temp1=temp1->link;
}
temp1->link=temp;
}
}
}
void insbet()
{
node *temp,*temp1;
int count=1,tnode=1;
temp=new node;
if(temp==0)
cout<<"\nOverflow";
else
{
int data,pos;
cout<<"Enter the data you want to insert: ";
cin>>data;
cout<<"Enter the position";
cin>>pos;
temp->data=data;
temp1=head;
while(temp1->link!=head)
{
tnode++;
temp1=temp1->link;
}
if(pos==1)
{
temp->link=head;
head=temp;
temp1->link=head;
}
else
{
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
while(count!=pos-1)
{
count++;
temp1=temp1->link;
}
temp->link=temp1->link;
temp1->link=temp;
}
}
}
}
void delbeg()
{
node *temp,*temp1;
if(head==0)
cout<<"\nLink List does not exist";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
temp1=head;
while(temp1->link!=head)
temp1=temp1->link;
head=temp->link;
temp1->link=head;
delete temp;
}
}
}
void delend()
{
node *temp,*temp1;
if(head==0)
cout<<"No Link List";
else
{
temp=head;
if(temp->link==head)
{
head=0;
delete temp;
}
else
{
while(temp->link!=head)
{
temp1=temp;
temp=temp->link;
}
temp1->link=head;
delete temp;
}
}
}
void delbet()
{
int pos;
cout<<"Enter the position";
cin>>pos;
node *temp,*temp1;
int count=1,tnode=1;
if(head==0)
cout<<"\nNo Link Iist";
else
{
temp=head;
while(temp->link!=head)
{
tnode++;
temp=temp->link;
}
if(tnode<pos)
cout<<"\nPosition does not exist";
else
{
temp1=head;
if(pos==1)
{
head=temp1->link;
temp->link=head;
delete temp1;
}
else
{
temp=head;
while(count!=pos)
{
count++;
temp1=temp;
temp=temp->link;
}
temp1->link=temp->link;
delete temp;
}
}
}
}
void main()
{
clrscr();
int ch;
char ans;
cout<<"\nMENU";
cout<<"\n1).Insert in the Begining";
cout<<"\n2).Insert in the End";
cout<<"\n3).Insert in between ";
cout<<"\n4).Delete from the Begining";
cout<<"\n5).Delete from the End";
cout<<"\n6).Delete from the between";
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:insbeg();
       display();
       break;
case 2:insend();
       display();
       break;
case 3:insbet();
       display();
       break;
case 4:delbeg();
       display();
       break;
case 5:delend();
       display();
       break;
case 6:delbet();
       display();
       break;
default :cout<<"\n Wrong choice";
}
cout<<"\nDo you want more press y for yes: ";
cin>>ans;
}while(ans=='y');
getch();
}

 

No comments:

Post a Comment