0% found this document useful (0 votes)
2 views

Doubled_ended_queue

The document contains a C program that implements a circular queue with operations to insert and delete elements from both the front and rear. It includes functions for displaying the queue and handling user input for various operations. The program prompts the user to enter the size of the queue and provides a menu for interaction until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Doubled_ended_queue

The document contains a C program that implements a circular queue with operations to insert and delete elements from both the front and rear. It includes functions for displaying the queue and handling user input for various operations. The program prompts the user to enter the size of the queue and provides a menu for interaction until the user chooses to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
int queue[20];
int size;
int front=-1;
int rear=-1;
void InsertAtFront(int item)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Queue is full\n");
return;
}
if( front == -1)
{
front=0;
rear=0;
}
else if(front==0)
{
front=size-1;
}
else{
front=front-1;
}
queue[front]=item;
}
void InsertAtRear(int item)
{
if((front==0 && rear==size-1) || (front==rear+1))
{
printf("Queue is full\n");
return;
}
if( rear == -1)
{
front=0;
rear=0;
}
else if(rear==size-1)
{
rear=0;
}
else{
rear++;
}
queue[rear]=item;
}
void deleteAtFront()
{
if(front == -1)
{
printf("Nothing to delete\n");
return;
}
if(front==rear)
{
front=rear=-1;
}
else if(front==size-1){
front=0;
}
else{
front++;
}
}
void deleteAtRear()
{

if(front == -1)
{
printf("Nothing to delete\n");
return;
}
if(front==rear)
{
front=rear=-1;
}
else if(rear==0){
rear=size-1;
}
else{
rear--;
}
}
void display()
{
if(front==-1)
{
printf("Nothing to display");
return;
}
int i= front;
while(i!=rear)
{
printf("%d\t",queue[i]);
i=(i+1)%size;
}
printf("%d\t",queue[i]);
}
void main()
{
int option,item;
printf("Enter the size of the queue");
scanf("%d",&size);
do
{
printf("\nEnter your option\n1.Insert At Front\n2.Insert At Rear\
n3.Delete At Front\n4.Delete At Rear\n5.Display\n6.Exit\n");
{
case 1:
printf("Enter the element\n");
scanf("%d",&item);
InsertAtFront(item);
break;
case 2:
printf("Enter the element\n");
scanf("%d",&item);
InsertAtRear(item);
break;
case 3:
deleteAtFront();
break;
case 4:
deleteAtRear();
break;
case 5:
display();
break;
case 6:
printf("Exiting...");
break;
default:
printf("Enter a valid option\n");
break;
}
} while(option!=6);
}

You might also like