双端队列的出队顺序图
Double ended queue is a more generalized form of queue data structure which allows insertion and removal of elements from both the ends, i.e , front and back.
双头队列是队列数据结构的一种更通用的形式,它允许从两端(即正面和背面)插入和删除元素。
双头队列的实现 (Implementation of Double ended Queue)
Here we will implement a double ended queue using a circular array. It will have the following methods:
在这里,我们将使用循环数组实现双端队列。 它将具有以下方法:
push_back : inserts element at back
push_back:在后面插入元素
push_front : inserts element at front
push_front:在前面插入元素
pop_back : removes last element
pop_back:删除最后一个元素
pop_front : removes first element
pop_front:删除第一个元素
get_back : returns last element
get_back:返回最后一个元素
get_front : returns first element
get_front:返回第一个元素
empty : returns true if queue is empty
空:如果队列为空,则返回true
full : returns true if queue is full
full:如果队列已满,则返回true
// Maximum size of array or Dequeue
#define SIZE 5
class Dequeue
{
//front and rear to store the head and tail pointers
int *arr;
int front, rear;
public :
Dequeue()
{
//Create the array
arr = new int[SIZE];
//Initialize front and rear with -1
front = -1;
rear = -1;
}
// Operations on Deque
void push_front(int );
void push_back(int );
void pop_front();
void pop_back();
int get_front();
int get_back();
bool full();
bool empty();
};
在前面插入元素 (Insert Elements at Front)
First we check if the queue is full. If its not full we insert an element at front end by following the given conditions :
首先,我们检查队列是否已满。 如果未满,我们按照给定条件在前端插入一个元素:
If the queue is empty then intialize front and rear to 0. Both will point to the first element.
如果队列为空,则将前和后初始化为0。两者都将指向第一个元素。
Else we decrement front and insert the element. Since we are using circular array, we have to keep in mind that if front is equal to 0 then instead of decreasing it by 1 we make it equal to SIZE-1.
否则,我们递减front并插入元素。 由于我们使用的是圆形数组,因此必须记住,如果front等于0,那么与其将其减1而不是使其等于SIZE-1。
void Dequeue :: push_front(int key)
{
if(full())
{
cout << "OVERFLOW\n";
}
else
{
//If queue is empty
if(front == -1)
front = rear = 0;
//If front points to the first position element
else if(front == 0)
front = SIZE-1;
else
--front;
arr[front] = key;
}
}
在后面插入元素 (Insert Elements at back)
Again we check if the queue is full. If its not full we insert an element at back by following the given conditions:
再次,我们检查队列是否已满。 如果未满,我们按照给定条件在后面插入一个元素:
If the queue is empty then intialize front and rear to 0. Both will point to the first element.
如果队列为空,则将前和后初始化为0。两者都将指向第一个元素。
Else we increment rear and insert the element. Since we are using circular array, we have to keep in mind that if rear is equal to SIZE-1 then instead of increasing it by 1 we make it equal to 0.
否则,我们增加后方并插入元素。 由于我们使用的是圆形数组,因此必须记住,如果后方等于SIZE-1,那么与其将其增加1而不是使其等于0。
void Dequeue :: push_back(int key)
{
if(full())
{
cout << "OVERFLOW\n";
}
else
{
//If queue is empty
if(front == -1)
front = rear = 0;
//If rear points to the last element
else if(rear == SIZE-1)
rear = 0;
else
++rear;
arr[rear] = key;
}
}
删除第一个元素 (Delete First Element)
In order to do this, we first check if the queue is empty. If its not then delete the front element by following the given conditions :
为此,我们首先检查队列是否为空。 如果不是,则按照给定条件删除front元素:
If only one element is present we once again make front and rear equal to -1.
如果仅存在一个元素,则我们再次使前后等于-1。
Else we increment front. But we have to keep in mind that if front is equal to SIZE-1 then instead of increasing it by 1 we make it equal to 0.
否则,我们增加前面。 但是我们必须记住,如果front等于SIZE-1,那么与其将其增加1而不是使其等于0。
void Dequeue :: pop_front()
{
if(empty())
{
cout << "UNDERFLOW\n";
}
else
{
//If only one element is present
if(front == rear)
front = rear = -1;
//If front points to the last element
else if(front == SIZE-1)
front = 0;
else
++front;
}
}
删除最后一个元素 (Delete Last Element)
Inorder to do this, we again first check if the queue is empty. If its not then we delete the last element by following the given conditions :
为此,我们再次首先检查队列是否为空。 如果不是,则按照给定条件删除最后一个元素:
If only one element is present we make front and rear equal to -1.
如果仅存在一个元素,则使前后等于-1。
Else we decrement rear. But we have to keep in mind that if rear is equal to 0 then instead of decreasing it by 1 we make it equal to SIZE-1.
否则我们减少后方。 但是我们必须记住,如果后方等于0,那么与其将其减1而不是使其等于SIZE-1。
void Dequeue :: pop_back()
{
if(empty())
{
cout << "UNDERFLOW\n";
}
else
{
//If only one element is present
if(front == rear)
front = rear = -1;
//If rear points to the first position element
else if(rear == 0)
rear = SIZE-1;
else
--rear;
}
}
检查队列是否为空 (Check if Queue is empty)
It can be simply checked by looking where front points to. If front is still intialized with -1, the queue is empty.
只需查看前端指向的位置即可对其进行检查。 如果front仍使用-1初始化,则队列为空。
bool Dequeue :: empty()
{
if(front == -1)
return true;
else
return false;
}
检查队列是否已满 (Check if Queue is full)
Since we are using circular array, we check for following conditions as shown in code to check if queue is full.
由于我们使用的是循环数组,因此我们检查代码中所示的以下条件,以检查队列是否已满。
bool Dequeue :: full()
{
if((front == 0 && rear == SIZE-1) ||
(front == rear + 1))
return true;
else
return false;
}
返回第一个元素 (Return First Element)
If the queue is not empty then we simply return the value stored in the position which front points.
如果队列不为空,那么我们只返回存储在前端位置的值。
int Dequeue :: get_front()
{
if(empty())
{ cout << "f=" <<front << endl;
cout << "UNDERFLOW\n";
return -1;
}
else
{
return arr[front];
}
}
返回最后一个元素 (Return Last Element)
If the queue is not empty then we simply return the value stored in the position which rear points.
如果队列不为空,那么我们只返回存储在后面位置的值。
int Dequeue :: get_back()
{
if(empty())
{
cout << "UNDERFLOW\n";
return -1;
}
else
{
return arr[rear];
}
}
翻译自: https://www.studytonight.com/data-structures/double-ended-queue
双端队列的出队顺序图