Dequeue | Data Structure | C Program
Dequeue:
Code:
#include<stdio.h>
#define MAX 5
int rear=-1;
int front=-1;
int cq[MAX];
void inf();
void inr();
void def();
void der();
void di();
void main()
{
int ch;
while(1)
{
printf("\nChoose any option\n1.Insert from front \t2.Insert from rear\n3.Delete from front\t4.Delete from rear\n5.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
inf();
break;
case 2:
inr();
break;
case 3:
def();
break;
case 4:
der();
break;
case 5:
di();
break;
}
}
}
void inf()
{
int item;
if((front==0 && rear==MAX-1) || front==rear+1)
printf("Queue is full\n");
else
{
printf("Enter a value\n");
scanf("%d",&item);
if(front==-1)
{
front=0;
rear=0;
}
else if(front==0)
{
front=MAX-1;
}
else
front=front-1;
cq[front]=item;
}
}
void der()
{
int item;
if(rear==-1)
printf("Queue Underflow!!\n");
else
{
item=cq[rear];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(rear==0)
rear==MAX-1;
else
rear=rear-1;
printf("%d is deleted\n",item);
}
}
void inr()
{
int item;
if((front==0 && rear==MAX-1) || front==rear+1)
{
printf("Queue is full!!\n");
}
else
{
printf("Enter a value\n");
scanf("%d",&item);
if(front==-1)
front=0,rear=0;
else if(rear==MAX-1)
rear=0;
else
rear=rear+1;
cq[rear]=item;
}
}
void def()
{
int item;
if(front==-1)
printf("Queue underflow!!\n");
else
{
item=cq[front];
printf("You deleted %d\n",item);
if(rear==front)
{
rear=-1;
front=-1;
}
else if(front==MAX-1)
{
front=0;
}
else
{
front=front+1;
}
}
}
void di()
{
int i;
if(front==-1)
{
printf("Queue is empty!!\n");
for(i=0;i<MAX;i++)
printf("_\t");
}
else
{
if(front<=rear)
{
for(i=0;i<front;i++)
printf("_\t");
for(i=front;i<=rear;i++)
printf("%d\t",cq[i]);
for(i=rear+1;i<MAX;i++)
printf("_\t");
}
else
{
for(i=0;i<=rear;i++)
printf("%d\t",cq[i]);
for(i=rear+1;i<front;i++)
printf("_\t");
for(i=front;i<MAX;i++)
printf("%d\t",cq[i]);
}
}
}
Output:
------------------------------------------------------------Circular Queue : >>> CQueue <<<
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)