0% found this document useful (0 votes)
9 views9 pages

De Queue

A deQueue (Double Ended Queue) allows insertion and removal of elements from both ends, differing from the traditional FIFO queue. There are two types of deQueues: Input Restricted (insertion at one end only) and Output Restricted (deletion at one end only). The document also includes C code implementations for various operations on a deQueue, such as insertion, deletion, and displaying elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

De Queue

A deQueue (Double Ended Queue) allows insertion and removal of elements from both ends, differing from the traditional FIFO queue. There are two types of deQueues: Input Restricted (insertion at one end only) and Output Restricted (deletion at one end only). The document also includes C code implementations for various operations on a deQueue, such as insertion, deletion, and displaying elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

deQueue

deQueue in Data Structures

Deque or Double Ended Queue is a type of queue in which insertion and removal of elements can
either be performed from the front or the rear. Thus, it does not follow FIFO rule (First In First Out).
Types of deQueue
★ Input Restricted Dequeue
In this dequeue, insertion is restricted at a single end but allows deletion at both the ends.

★ Output Restricted Dequeue


In this dequeue, deletion is restricted at a single end but allows insertion at both the ends.
Operations on a deQueue

★ Insertion at front
★ Insertion at rear
★ Deletion at front
★ Deletion at rear
We can also perform peek operations in the dequeue along with the
operations listed above. Through peek operation, we can get the front
and rear elements of the dequeue.
★ Get the front item from the
dequeue
★ Get the rear item from the dequeue
★ Check whether the dequeue is full
or not

★ Checks whether the dequeue is


empty or not
deQueue Code Implementation in C
int main() {
#include <stdio.h> int arr[MAX];
#define MAX 10 int front, rear, i, m,j,t;
front = rear = -1;
void addFront(int *, int, int *, int *); for (i = 0; i < MAX; i++)
void addRear(int *, int, int *, int *); arr[i] = 0;
int delFront(int *, int *, int *); addFront(arr, 30, &front, &rear);
int delRear(int *, int *, int *); addFront(arr, 25, &front, &rear);
void display(int *); addFront(arr, 20, &front, &rear);
int count(int *); addFront(arr, 15, &front, &rear);
addFront(arr, 10, &front, &rear);
addFront(arr, 5, &front, &rear);
display(arr);
scanf("%d",&m);
for(j=0;j<m;j++) {
scanf("%d",&t);
addRear(arr, t, &front, &rear);
}
display(arr);
delFront(arr,&front,&rear);
delRear(arr,&front,&rear);
printf("Elements in the deQueue after insertion and deletion:\n ");
display(arr);
}
deQueue Code Implementation in C
void addFront(int *arr, int item, int *pfront, int *prear) {
void addRear(int *arr, int item, int *pfront, int *prear) {
int i, k, c;
int i, k;
if (*pfront == 0 && *prear == MAX - 1) {
if (*pfront == 0 && *prear == MAX - 1) {
printf("deQueue is full.\n");
printf("deQueue is full.\n");
return; }
return; }
if (*pfront == -1) {
if (*pfront == -1) {
*pfront = *prear = 0;
*prear = *pfront = 0;
arr[*pfront] = item;
arr[*prear] = item;
return; }
return; }
if (*prear != MAX - 1) {
if (*prear == MAX - 1) {
c = count(arr);
k = *pfront - 1;
k = *prear + 1;
for (i = *pfront - 1; i < *prear; i++) {
for (i = 1; i <= c; i++) {
k = i;
arr[k] = arr[k - 1];
if (k == MAX - 1)
k--; }
arr[k] = 0;
arr[k] = item;
else
*pfront = k;
arr[k] = arr[i + 1]; }
(*prear)++;
(*prear)--;
} else {
(*pfront)--; }
(*pfront)--;
(*prear)++;
arr[*pfront] = item;
arr[*prear] = item;
}
}
}
deQueue Code Implementation in C
int delFront(int *arr, int *pfront, int *prear) {
int delRear(int *arr, int *pfront, int *prear) {
int item;
int item;

if (*pfront == -1) {
if (*pfront == -1) {
printf("deQueue is empty.\n");
printf("deQueue is empty.\n");
return 0;
return 0;
}
}

item = arr[*pfront];
item = arr[*prear];
arr[*pfront] = 0;
arr[*prear] = 0;
(*prear)--;
if (*pfront == *prear)
if (*prear == -1)
*pfront = *prear = -1;
*pfront = -1;
else
return item;
(*pfront)++;
}

return item;
}
deQueue Code Implementation in C
void display(int *arr) {
int count(int *arr) {
int i;
int c = 0, i;

printf(" front: ");


for (i = 0; i < MAX; i++) {
for (i = 0; i < MAX; i++)
if (arr[i] != 0)
printf(" %d", arr[i]);
c++;
printf(" :rear\n");
}
}
return c;
}
deQueue Code Implementation in C with output

OUTPUT : front: 5 10 15 20 25 30 0 0 0
0 :rear
2
35
40
front: 5 10 15 20 25 30 35 40 0
0 :rear
Elements in the deQueue after insertion
and deletion:
front: 0 10 15 20 25 30 35 0 0
0 :rear

You might also like