WAP TO IMPLEMENT
CIRCULAR QUEUE
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct
node
{
int data;
struct node *next;
}node;
void init(node **R);
void enqueue(node **R,int x);
int dequeue(node **R);
int empty(node *rear);
void print(node *rear);
int front_element(node *rear);
int rear_element(node *rear);
void main()
{
int x,option;
int i;
node *rear;
init(&rear);
do
{
printf("\n1.insert.\n2.delete\n3.print");
printf("\n4.front elment\n5.rear
element\n6.quit");
printf("\n your option:");
scanf("%d",&option);
switch(option)
{
case 1:printf("\n enter queue
data:");
scanf("%d",&x);\
enqueue(&rear,x);
break;
case 2:if(!empty(rear))
{
x=dequeue(&rear);
printf("\n element deleted
=%d",x);
}
else
printf("\n underflow..........cannot
delete");
break;
case 3:print(rear);
break;
case 4:if(!empty(rear))
printf("\n front element
=%d",front_element(rear));
else
printf("\n queue is empty");
break;
case 5:if(!empty(rear))
printf("\n rear element=%d",rear_element(rear));
else
printf("\n queueu is empty");
break;
}
}while(option!=6);
}
void init(node **R)
{
*R=NULL;
}
void enqueue(node **R,int x)
{
node *p;
p=(node*)malloc(sizeof(node));
p->data=x;
if(empty(*R))
{
p->next=p;
*R=p;
}
else
{
p->next=(*R)->next;
(*R)->next=p;
(*R)=p;
}
}
int dequeue(node **R)
{
int x;
node *p;
p=(*R)->next;
x=p->data;
if(p->next==p)
{
*R=NULL;
free(p);
return(x);
}
(*R)->next=p->next;
free(p);
return(x);
}
void print(node *rear)
{
node *p;
if(!empty(rear))
{
p=rear->next;
}
else
{
printf("\n queue is empty");
return;
}
printf("\n");
do
{
printf("%d",p->data);
p=p->next;
}
while(p!=rear->next);
}
int empty(node *P)
{
if(P->next==NULL)
return(1);
return(0);
}
int front_element(node *rear)
{
return(rear->next->data);
}
int rear_element(node *rear)
{
return(rear->data);
}
No comments:
Post a Comment