Double Ended Queuedocx

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Implementation of Deque(Double-Ended Queue)

#include <stdio.h> #include <conio.h> #define MAXSIZE 5 static int dq[MAXSIZE]; int front=MAXSIZE/2,rear=MAXSIZE/2; void add_at_front(int data); void add_at_rear(int data); int del_from_front(); int del_from_rear(); void cannot_add_at_front(); void cannot_add_at_rear(); void display_queue(); void queue_empty(); void main() { int data; char c='1'; clrscr(); while(c!='5') { printf("\nThe present queue is"); display_queue(); printf("\n1.ADD AT FRONT"); printf("\n2.ADD AT REAR"); printf("\n3.DEL FROM FRONT"); printf("\n4.DEL FROM REAR"); printf("\n5.EXIT"); printf("\nEnter your choice"); fflush(stdin); c=getche(); if(c=='1') { printf("\nEnter the element to be added at front"); scanf("%d",&data); add_at_front(data); } if(c=='2') { printf("\nEnter the element to be added at rear"); scanf("%d",&data); add_at_rear(data); } if(c=='3') { data=del_from_front(); printf("\nThe deleted element is %d",data); } if(c=='4') { data=del_from_rear(); printf("\nThe deleted element is %d",data);

} } } void add_at_front(int data) { if(front==-1) cannot_add_at_front(); else { dq[front]=data; front=front-1; } } void add_at_rear(int data) { if(rear==MAXSIZE-1) cannot_add_at_rear(); else { rear=rear+1; dq[rear]=data; } } int del_from_front() { int data; if((rear-front)==0) { queue_empty(); return(0); } else { front=front+1; data=dq[front]; dq[front]=0; return(data); } } int del_from_rear() { int data; if(rear-front==0) { queue_empty(); return(0); } else { data=dq[rear]; dq[rear]=0; rear=rear-1; return(data); } } void cannot_add_at_front() {

printf("\nCannot add at front"); } void cannot_add_at_rear() { printf("\nCannot add at rear"); } void queue_empty() { printf("\nThere is nothing in the queue to delete"); } void display_queue() { int i; for(i=0;i<MAXSIZE;i++) printf("%3d",dq[i]); }

You might also like