0% found this document useful (0 votes)
18 views5 pages

Deque Implementation with Arrays in C

Uploaded by

Kishore Chandran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Deque Implementation with Arrays in C

Uploaded by

Kishore Chandran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEQUE USING ARRAYS

#include<stdio.h>

#include<stdlib.h>

#define MAX 5

int DQ[MAX],item,front=-1,rear=-1;

//Inserting item at the front end

void push_dq(int item)

if(front==0)

printf("\nNo space at front, Cant insert....\n");

else

if(front==-1)

front = 0;

rear = 0;

DQ[front] = item;

else

//decerement front and insert

front--;

DQ[front]=item;

}
//Removing item from the front end

int pop_dq()

int itm;

if(front==-1)

printf("\nEmpty queue..No deletion possible...\n");

else if(front==rear)

itm = DQ[front];

front = -1;

rear = -1;

else

itm = DQ[front];

front++;

return itm;

//Insert item at the rear

void inject(int item)

//Normal insertion in a Queue

if(rear==MAX-1)

printf("\nQueue full..No insertions possible..");

else if(rear==-1)

{
//Inserting first element

front = 0;

rear = 0;

DQ[rear]= item;

else

rear++;

DQ[rear]=item;

//Delete item from rear end

int eject()

int itm;

if(rear==-1)

printf("\nDeque is empty..Nothing to eject...\n");

return 0;

else if(rear==front)

itm = DQ[rear];

rear = -1;

front = -1;

else
{

itm = DQ[rear];

rear--;

return itm;

void display_dq()

int i;

if(front==-1)

printf("\nDeque is empty");

else

printf("\nThe deque is ");

for(i=front;i<=rear;i++)

printf("\t%d",DQ[i]);

void main()

int ch;

do

printf("\nMenu\[Link] at front\[Link] at rear \[Link] from front\[Link]


from rear\[Link] from front\[Link]\n");

printf("Enter choice : ");

scanf("%d", &ch);

switch(ch)
{

case 1: printf("\nEnter the item to insert :");

scanf("%d",&item);

push_dq(item);

break;

case 2: printf("\nEnter item to be inserted at front end : ");

scanf("%d",&item);

inject(item);

break;

case 3: printf("\nDeletion from front end");

item=pop_dq();

printf("\nDeleted %d",item);

break;

case 4: printf("\nDeletion from rear end");

item=eject();

printf("\nDeleted %d",item);

break;

case 5: //printf("\nThe deque is");

display_dq();

break;

case 6: exit(0);

}while(ch!=0);

You might also like