双端队列(deque,即double-ended queue的缩写)是一种具有队列和栈性质的数据结构,即可以(也只能)在线性表的两端进行插入和删除。若以顺序存储方式实现双端队列,请编写例程实现下列操作:
Push(X,D):将元素X插入到双端队列D的头;
Pop(D):删除双端队列D的头元素,并返回;
Inject(X,D):将元素X插入到双端队列D的尾部;
Eject(D):删除双端队列D的尾部元素,并返回。
函数接口定义:
bool Push( ElementType X, Deque D );
ElementType Pop( Deque D );
bool Inject( ElementType X, Deque D );
ElementType Eject( Deque D );
其中Deque结构定义如下:
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef PtrToQNode Deque;
注意:Push和Inject应该在正常执行完操作后返回true,或者在出现非正常情况时返回false。当Front和Rear相等时队列为空,Pop和Eject必须返回由裁判程序定义的ERROR。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR -1
typedef int ElementType;
typedef enum { push, pop, inject, eject, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
ElementType *Data; /* 存储元素的数组 */
Position Front, Rear; /* 队列的头、尾指针 */
int MaxSize; /* 队列最大容量 */
};
typedef PtrToQNode Deque;
Deque CreateDeque( int MaxSize )
{ /* 注意:为区分空队列和满队列,需要多开辟一个空间 */
Deque D = (Deque)malloc(sizeof(struct QNode));
MaxSize++;
D->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
D->Front = D->Rear = 0;
D->MaxSize = MaxSize;
return D;
}
bool Push( ElementType X, Deque D );
ElementType Pop( Deque D );
bool Inject( ElementType X, Deque D );
ElementType Eject( Deque D );
Operation GetOp(); /* 裁判实现,细节不表 */
void PrintDeque( Deque D ); /* 裁判实现,细节不表 */
int main()
{
ElementType X;
Deque D;
int N, done = 0;
scanf("%d", &N);
D = CreateDeque(N);
while (!done) {
switch(GetOp()) {
case push:
scanf("%d", &X);
if (!Push(X, D)) printf("Deque is Full!\n");
break;
case pop:
X = Pop(D);
if ( X==ERROR ) printf("Deque is Empty!\n");
else printf("%d is out\n", X);
break;
case inject:
scanf("%d", &X);
if (!Inject(X, D)) printf("Deque is Full!\n");
break;
case eject:
X = Eject(D);
if ( X==ERROR ) printf("Deque is Empty!\n");
else printf("%d is out\n", X);
break;
case end:
PrintDeque(D);
done = 1;
break;
}
}
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
Pop
Inject 1
Pop
Eject
Push 2
Push 3
Eject
Inject 4
Inject 5
Inject 6
Push 7
Pop
End
输出样例:
Deque is Empty!
1 is out
Deque is Empty!
2 is out
Deque is Full!
Deque is Full!
3 is out
Inside Deque: 4 5
这道题的难点在于如何探求Rear与Front的位置
由普通队列的知识我们可以知道,front = (front + 1) % maxsize rear = (rear + 1) % maxsize
在这里,rear可以沿用,但是front需要变化,由3.12的解析可知,当队列中的下标向前变动时
需要(front - 1 + maxsize)% maxsize 这样可以保证循环且不溢出
最后就是一个细节,rear对应的就是下一个元素应当存放的下标,存完后才跳到下一个下标准备接收下一个元素
因此rear在删除时是向左移一个下标,然后再删除,但在添加时是直接添加元素,然后右移一个下标
而front不一样,front所代表下标直接指向要删除的元素,因此若要向左添加元素,要先将front向左移然后再插入元素
在删除时是先直接删除然后再向右移
front和rear的不同操作会导致原本长得很像的两个函数有所区别,这是特别要注意的地方
下面给出AC代码。欢迎各位大佬提出修改意见或指出错误!
bool Push( ElementType X, Deque D )
{
if ((D->Rear + 1) % D->MaxSize == D->Front)
//这是判断是否满队列的式子
//即当队列满的时候,rear应该是在front左边,加1则会重合,重合便代表队列满
return false;
else
{//记住front是先向前移一个下标然后存入元素
D->Front = (D->Front - 1 + D->MaxSize) % D->MaxSize;
D->Data[D->Front] = X;
return true;
}
}
ElementType Pop( Deque D )
{
int place;
if (D->Rear == D->Front)//判断是否队列空
return ERROR;
else
{//记住front是直接删除然后向右移
place = D->Front;
D->Front = (D->Front + 1) % D->MaxSize;
return D->Data[place];
}
}
bool Inject( ElementType X, Deque D )
{
if ((D->Rear + 1) % D->MaxSize == D->Front)
return false;
else
{//rear是先添加新元素然后向右移一个下标
D->Data[D->Rear] = X;
D->Rear = (D->Rear + 1) % D->MaxSize;
return true;
}
}
ElementType Eject( Deque D )
{
int place;
if (D->Rear == D->Front)
return ERROR;
else
{//rear是先左移一个元素然后删除,因此不用place标记
D->Rear = (D->Rear - 1 + D->MaxSize) % D->MaxSize;
return D->Data[D->Rear];
}
}