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

Double Ended Queue Through One Dimensional Array

The document contains an implementation of a Double Ended Queue (Deque) using a one-dimensional array in C. It includes functions for inserting and deleting elements from both the front and rear, along with checks for overflow and underflow conditions. The main function demonstrates the usage of these operations with sample outputs.

Uploaded by

Kedar Ghadge
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)
5 views5 pages

Double Ended Queue Through One Dimensional Array

The document contains an implementation of a Double Ended Queue (Deque) using a one-dimensional array in C. It includes functions for inserting and deleting elements from both the front and rear, along with checks for overflow and underflow conditions. The main function demonstrates the usage of these operations with sample outputs.

Uploaded by

Kedar Ghadge
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
You are on page 1/ 5

Assignment No.

Name – Siddhesh Shivaji Kumbhar Roll No – 100

Implementation of Double Ended Queue through One


Dimensional Array
Code -
#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int deque[MAX];
int front = -1;
int rear = -1;

int isFull() {
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}
int isEmpty() {
return (front == -1);
}
void insertFront(int key) {
if (isFull()) {
printf("Overflow: Unable to insert element at the front. Deque is full.\n");
return;
}

if (front == -1) {
front = 0;

1
Assignment No.7

rear = 0;
} else if (front == 0) {
front = MAX - 1;
} else {
front = front - 1;
}

deque[front] = key;
printf("Inserted %d at the front.\n", key);
}
void insertRear(int key) {
if (isFull()) {
printf("Overflow: Unable to insert element at the rear. Deque is full.\n");
return;
}

if (rear == -1) {
front = 0;
rear = 0;
} else if (rear == MAX - 1) {
rear = 0;
} else {
rear = rear + 1;
}

deque[rear] = key;
printf("Inserted %d at the rear.\n", key);
}
void deleteFront() {
if (isEmpty()) {

2
Assignment No.7

printf("Underflow: Unable to delete element from the front. Deque is empty.\n");


return;
}

int removed = deque[front];

if (front == rear) {
front = -1;
rear = -1;
} else if (front == MAX - 1) {
front = 0;
} else {
front = front + 1;
}

printf("Deleted %d from the front.\n", removed);


}
void deleteRear() {
if (isEmpty()) {
printf("Underflow: Unable to delete element from the rear. Deque is empty.\n");
return;
}

int removed = deque[rear];

if (front == rear) {
front = -1;
rear = -1;
} else if (rear == 0) {
rear = MAX - 1;

3
Assignment No.7

} else {
rear = rear - 1;
}

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


}
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");
}
int main() {
insertRear(5);
displayDeque();

insertFront(15);
displayDeque();

insertRear(25);

4
Assignment No.7

displayDeque();
deleteFront();
displayDeque();
deleteRear();
displayDeque();

return 0;
}

OUTPUT –

You might also like