Circular queue by linked list

//Circular queue by linked list
#include
#include
#include
#include
#include
struct cllqueue
{
int info;
cllqueue *next;
};
cllqueue *start;
void insert()
{
cllqueue *p,*q;
p=(cllqueue*)malloc(sizeof(cllqueue));
cout<<"\nEnter an element to insert:";
scanf("%d",&p->info);
if(start==NULL)
{
start=p;
p->next=start;
}
else
{
q=start;
while(q->next!=start)
{
q=q->next;
}
q->next=p;
p->next=start;
}
}

void delet()
{
cllqueue *p,*q;
p=start;
if(start==NULL)
{
cout<<"\nQueue is underflow";
}
else
if(p==p->next)
{
start=NULL;
cout<<"\nElement is deleted, now queue is empty";
}
else
{
q=start;
while(q->next!=start)
{
q=q->next;
}
start=p->next;
q->next=start;
cout<<"\nElement is deleted";
}
}

void display()
{
cllqueue *p;
p=start;
cout<<"\nCircular queue is:\n";
while(p->next!=start)
{
printf("%d\t",p->info);
p=p->next;
}
if(p->next==start)
{
printf("%d",p->info);
}
}

void main()
{
int ch;
clrscr();
while(1)
{
cout<<"\n1:Insertion";
cout<<"\n2:Deletion";
cout<<"\n3:Display";
cout<<"\0:Exit";

cout<<"\nEnter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
insert();
getch();
break;

case 2:
delet();
getch();
break;

case 3:
display();
getch();
break;

default:exit(0);
}
}
getch();
}

Popular Posts