3-1 Deque (20分)
(先进先出的特点)
A “deque” is a data structure consisting of a list of items, on which the following operations are possible:
Push(X,D): Insert item X on the front end of deque D.
Pop(D): Remove the front item from deque D and return it.
Inject(X,D): Insert item X on the rear end of deque D.
Eject(D): Remove the rear item from deque D and return it. Write routines to support the deque that take O(1) time per operation.
“deque”是一种数据结构,由一系列项目组成,在该列表上可以进行以下操作:
Push(X,D):将项目X插入deque D的前端。
Pop(D):从deque D中移除前面的项并将其返回。
Inject(X,D):将X项插入deque D的后端。
Eject(D):从dequeD中取出后部项目并返回。
编写例程以支持每次操作花费O(1)时间的deque。
Format of functions:
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );
where Deque is defined as the following:
typedef struct Node *PtrToNode;
struct Node {
ElementType Element;
PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
PtrToNode Front, Rear;
};
Here the deque is implemented by a doubly linked list with a header. Front and Rear point to the two ends of the deque respectively. Front always points to the header. The deque is empty when Front and Rear both point to the same dummy header. Note: Push and Inject are supposed to return 1 if the operations can be done successfully, or 0 if fail. If the deque is empty, Pop and Eject must return ERROR which is defined by the judge program.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
#define ElementType int
#define ERROR 1e5
typedef enum { push, pop, inject, eject, end } Operation;
typedef struct Node *PtrToNode;
struct Node {
ElementType Element;
PtrToNode Next, Last;
};
typedef struct DequeRecord *Deque;
struct DequeRecord {
PtrToNode Front, Rear;
};
Deque CreateDeque();
int Push( ElementType X, Deque D );
ElementType Pop( Deque D );
int Inject( ElementType X, Deque D );
ElementType Eject( Deque D );
Operation GetOp(); /* details omitted */
void PrintDeque( Deque D ); /* details omitted */
int main()
{
ElementType X;
Deque D;
int done = 0;
D = CreateDeque();
while (!done) {
switch(GetOp()) {
case push:
scanf("%d", &X);
if (!Push(X, D)) printf("Memory is Full!\n");
break;
case pop:
X = Pop(D);
if ( X==ERROR ) printf("Deque is Empty!\n");
break;
case inject:
scanf("%d", &X);
if (!Inject(X, D)) printf("Memory is Full!\n");
break;
case eject:
X = Eject(D);
if ( X==ERROR ) printf("Deque is Empty!\n");
break;
case end:
PrintDeque(D);
done = 1;
break;
}
}
return 0;
}
/* Your function will be put here */
Sample Input:
Pop
Inject 1
Pop
Eject
Push 1
Push 2
Eject
Inject 3
End
Sample Output:
Deque is Empty!
Deque is Empty!
Inside Deque: 2 3
AC啦:
这里还要提示,第一次做到有提示的题,记录一下。
这是第一次写队列的题,居然还是一个双向链表,我要晕了。。。
我这么笨,当然不可能一下子写出来了,单链表的队列一个没做过,直接来写这个,我人傻了,什么也看不懂。
Deque CreateDeque() {
Deque D;
PtrToNode p;
D=(Deque)malloc(sizeof(struct DequeRecord));
p=(PtrToNode)malloc(sizeof(struct Node));
p->Last=NULL;//刚开始的时候我写的是指向它本身,但是不对啊
p->Next=NULL;//这是一个双向链表
D->Front=p;
D->Rear=p;
return D;
}
int Push( ElementType X, Deque D ) {
//从头部插入节点
PtrToNode p;
p=(PtrToNode)malloc(sizeof(struct Node));
p->Element=X;
if(D->Front==D->Rear) {
//头尾指针指向相同位置,队列为空
p->Next=NULL;
p->Last=D->Front;
D->Front->Next=p;
D->Rear=p;
} else {
p->Next=D->Front->Next;
p->Last=D->Front;
D->Front->Next=p;
p->Next->Last=p;
}
return 1;
}
ElementType Pop( Deque D ) {
//从deque D中移除前面的项并将其返回。
if(D->Front==D->Rear)
return ERROR;
PtrToNode p;
int x;
p=D->Front->Next;
x=p->Element;
if(D->Front->Next==D->Rear) {
//对列中只有一个元素时候
D->Front->Next==NULL;
D->Rear=D->Front;
} else {
D->Front->Next=p->Next;
p->Next->Last=D->Front;
}
free(p);
return 1;
}
int Inject( ElementType X, Deque D ) {
//将X项插入D的后端
PtrToNode p=(PtrToNode)malloc(sizeof(struct Node));
p->Element=X;
p->Last=D->Rear;
p->Next=NULL;
D->Rear->Next=p;
D->Rear=p;
return 1;
}
ElementType Eject( Deque D ) {
//从D中取出后部项目并返回
if(D->Front==D->Rear) {
return ERROR;
}
int x;
PtrToNode p=D->Rear;
p->Last->Next=NULL;
D->Rear=p->Last;
x=p->Element;
free(p);
return x;
}