0% found this document useful (0 votes)
2 views2 pages

C Deque Implementation Example

Uploaded by

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

C Deque Implementation Example

Uploaded by

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

#include <stdio.

h>
#define MAX 5

int dq[MAX];
int front = -1, rear = -1;

int isFull() {
return (front == (rear + 1) % MAX);
}
int isEmpty() {
return (front == -1);
}

void insertFront(int val) {


if (isFull()) { printf("Deque Overflow\n"); return; }
if (isEmpty()) front = rear = 0;
else front = (front - 1 + MAX) % MAX;
dq[front] = val;
printf("Inserted %d at front\n", val);
}

void insertRear(int val) {


if (isFull()) { printf("Deque Overflow\n"); return; }
if (isEmpty()) front = rear = 0;
else rear = (rear + 1) % MAX;
dq[rear] = val;
printf("Inserted %d at rear\n", val);
}

int deleteFront() {
if (isEmpty()) { printf("Deque Underflow\n"); return -1; }
int val = dq[front];
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
return val;
}

int deleteRear() {
if (isEmpty()) { printf("Deque Underflow\n"); return -1; }
int val = dq[rear];
if (front == rear) front = rear = -1;
else rear = (rear - 1 + MAX) % MAX;
return val;
}

void display() {
if (isEmpty()) { printf("Deque is empty\n"); return; }
printf("Deque: ");
int i = front;
while (1) {
printf("%d ", dq[i]);
if (i == rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main() {
insertFront(100);
insertFront(200);
insertRear(10);
insertRear(20);
insertFront(5);
display();
printf("Deleted from front: %d\n", deleteFront());
printf("Deleted from rear: %d\n", deleteRear());
display();
return 0;
}

You might also like