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

Deque Operations in C Programming

Uploaded by

cs.ramgopal7036
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)
32 views2 pages

Deque Operations in C Programming

Uploaded by

cs.ramgopal7036
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> void insertRear(int key) {


#include <stdlib.h> if (isFull()) {
printf("Overflow: Unable to insert
#define MAX 5 // Define maximum size of element at the rear. Deque is full.\n");
the deque return;
}
int deque[MAX];
int front = -1; if (rear == -1) { // If deque is initially
int rear = -1; empty
front = 0;
// Function to check if the deque is full rear = 0;
int isFull() { } else if (rear == MAX - 1) {
return ((front == 0 && rear == MAX - 1) rear = 0; // wrap around
|| (front == rear + 1)); } else {
} rear = rear + 1;
}
// Function to check if the deque is empty
int isEmpty() { deque[rear] = key;
return (front == -1); printf("Inserted %d at the rear.\n", key);
} }

// Function to insert an element at the front // Function to delete an element from the
of the deque front of the deque
void insertFront(int key) { void deleteFront() {
if (isFull()) { if (isEmpty()) {
printf("Overflow: Unable to insert printf("Underflow: Unable to delete
element at the front. Deque is full.\n"); element from the front. Deque is empty.\n");
return; return;
} }

if (front == -1) { // If deque is initially int removed = deque[front];


empty
front = 0; if (front == rear) { // Deque has only one
rear = 0; element
} else if (front == 0) { front = -1;
front = MAX - 1; // wrap around rear = -1;
} else { } else if (front == MAX - 1) {
front = front - 1; front = 0; // wrap around
} } else {
front = front + 1;
deque[front] = key; }
printf("Inserted %d at the front.\n", key);
} printf("Deleted %d from the front.\n",
removed);
// Function to insert an element at the rear of }
the deque
// Function to delete an element from the displayDeque();
rear of the deque
void deleteRear() { insertFront(15);
if (isEmpty()) { displayDeque();
printf("Underflow: Unable to delete
element from the rear. Deque is empty.\n"); insertRear(25);
return; displayDeque();
}
deleteFront();
int removed = deque[rear]; displayDeque();

if (front == rear) { // Deque has only one deleteRear();


element displayDeque();
front = -1;
rear = -1;
} else if (rear == 0) { return 0;
rear = MAX - 1; // wrap around }
} else {
rear = rear - 1;
}

printf("Deleted %d from the rear.\n",


removed);
}

// Function to display the deque


void displayDeque() {
if (isEmpty()) {
printf("Deque is empty.\n");
return;
}

printf("Deque elements are: ");


int i = front;
while (1) {
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}

// Main function to test the operations


int main() {
insertRear(5);

You might also like