Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Deque (!!!)

A deque (double-ended queue) is a linear data structure that allows efficient insertion and removal of elements from both ends. It does not follow the FIFO rule of queues. Elements can be inserted and removed from either the front or rear. Common operations on a deque include insert at front, insert at rear, delete at front, and delete at rear. A deque is typically implemented using a circular array.

Uploaded by

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

Deque (!!!)

A deque (double-ended queue) is a linear data structure that allows efficient insertion and removal of elements from both ends. It does not follow the FIFO rule of queues. Elements can be inserted and removed from either the front or rear. Common operations on a deque include insert at front, insert at rear, delete at front, and delete at rear. A deque is typically implemented using a circular array.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

### deque

#DynamicStructure
[[Linked List]] [[BinaryTree (!!!)]] [[Stack (!!!)]] [[Queue (!)]] [[Deque
(!!!)]]

---
# Theory
---

![[Pasted image 20230320174910.png]]

Deque or Double Ended Queue is a type


of [queue](https://www.programiz.com/dsa/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).

Representation of Deque:
![[Pasted image 20230320172631.png]]
## Types of Deque

- **Input Restricted Deque**


In this deque, input is restricted at a single end but allows deletion at both
the ends.
- **Output Restricted Deque**
In this deque, output is restricted at a single end but allows insertion at
both the ends.

## Operations on a Deque

Below is [the circular array](https://www.programiz.com/dsa/circular-


queue) implementation of deque. In a circular array, if the array is full, we start
from the beginning.

But in a linear array implementation, if the array is full, no more elements can be
inserted. In each of the operations below, if the array is full, "overflow message"
is thrown.

Before performing the following operations, these steps are followed.

1. Take an array (deque) of size n.


2. Set two pointers at the first position and set `front = -1` and `rear = 0`.

Initialize an array and pointers for deque:


![[Pasted image 20230320172713.png]]
### 1. Insert at the Front

This operation adds an element at the front.

1. Check the position of front.


![[Pasted image 20230320172738.png]]
2. If `front < 1`, reinitialize `front = n-1` (last index).
Shift front to the end:
![[Pasted image 20230320172850.png]]
3. Else, decrease front by 1.
4. Add the new key 5 into `array[front]`.
Insert the element at Front:
![[Pasted image 20230320173009.png]]
### 2. Insert at the Rear

This operation adds an element to the rear.

1. Check if the array is full.


![[Pasted image 20230320173101.png]]
2. If the deque is full, reinitialize `rear = 0`.
3. Else, increase rear by 1.
![[Pasted image 20230320173116.png]]
4. Add the new key 5 into `array[rear]`.
![[Pasted image 20230320173145.png]]
### 3. Delete from the Front

The operation deletes an element from the front.

1. Check if the deque is empty.


![[Pasted image 20230320173204.png]]
2. If the deque is empty (i.e. `front = -1`), deletion cannot be performed
(**underflow condition**).
3. If the deque has only one element (i.e. `front = rear`), set `front = -
1` and `rear = -1`.
4. Else if front is at the end (i.e. `front = n - 1`), set go to the front `front
= 0`.
5. Else, `front = front + 1`.
![[Pasted image 20230320173230.png]]
### 4. Delete from the Rear

This operation deletes an element from the rear.

1. Check if the deque is empty.


![[Pasted image 20230320173301.png]]
2. If the deque is empty (i.e. `front = -1`), deletion cannot be performed
(**underflow condition**).
3. If the deque has only one element (i.e. `front = rear`), set `front = -
1` and `rear = -1`, else follow the steps below.
4. If rear is at the front (i.e. `rear = 0`), set go to the front `rear = n - 1`.
5. Else, `rear = rear - 1`.

![[Pasted image 20230320173325.png]]


### 5. Check Empty

This operation checks if the deque is empty. If `front = -1`, the deque is empty.

### 6. Check Full

This operation checks if the deque is full. If `front = 0` and `rear = n -


1` OR `front = rear + 1`, the deque is full.

---
# Implementation
---
```cpp
// Deque implementation in C++

#include <iostream>
using namespace std;

#define MAX 10
class Deque {
int arr[MAX];
int front;
int rear;
int size;

public:
Deque(int size) {
front = -1;
rear = 0;
this->size = size;
}

void insertfront(int key);


void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};

bool Deque::isFull() {
return ((front == 0 && rear == size - 1) ||
front == rear + 1);
}

bool Deque::isEmpty() {
return (front == -1);
}

void Deque::insertfront(int key) {


if (isFull()) {
cout << "Overflow\n"
<< endl;
return;
}

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

else if (front == 0)
front = size - 1;

else
front = front - 1;

arr[front] = key;
}

void Deque ::insertrear(int key) {


if (isFull()) {
cout << " Overflow\n " << endl;
return;
}
if (front == -1) {
front = 0;
rear = 0;
}

else if (rear == size - 1)


rear = 0;

else
rear = rear + 1;

arr[rear] = key;
}

void Deque ::deletefront() {


if (isEmpty()) {
cout << "Queue Underflow\n"
<< endl;
return;
}

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

else
front = front + 1;
}

void Deque::deleterear() {
if (isEmpty()) {
cout << " Underflow\n"
<< endl;
return;
}

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

int Deque::getFront() {
if (isEmpty()) {
cout << " Underflow\n"
<< endl;
return -1;
}
return arr[front];
}

int Deque::getRear() {
if (isEmpty() || rear < 0) {
cout << " Underflow\n"
<< endl;
return -1;
}
return arr[rear];
}

int main() {
Deque dq(4);

cout << "insert element at rear end \n";


dq.insertrear(5);
dq.insertrear(11);

cout << "rear element: "


<< dq.getRear() << endl;

dq.deleterear();
cout << "after deletion of the rear element, the new rear element: " <<
dq.getRear() << endl;

cout << "insert element at front end \n";

dq.insertfront(8);

cout << "front element: " << dq.getFront() << endl;

dq.deletefront();

cout << "after deletion of front element new front element: " << dq.getFront() <<
endl;
}
```

You might also like