3-1 Deque (20分) HBU-DS
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.
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:z
Deque is Empty!
Deque is Empty!
Inside Deque: 2 3
注意
node立马last指针指的就是上一个结点。需要注意,插入或者删除一个结点时,这个结点的next last,上一个结点的next,下一个结点的last都需要改变。
Push Pop操作都是在front处操作,但是因为front指的是头节点,并不需要改变front的值,要注意特殊情况:push操作前无数据结点;pop操作前只有一个数据结点。这两种情况操作后,rear的值需要改变,所以需要把这两种情况都拿出来分开写!
如果遇到段错误的话,有可能是在 pop push操作处出的问题。pop操作时,会把需要删除的结点的下一个结点(也就是第二个数据结点)的last置为front,但是有只有一个数据结点的情况,就可能会段错误。还有push操作时把操作前第一个结点的last置为当前插入结点,但是有没有数据结点的情况。
代码
Deque CreateDeque()
{
Deque dq = (Deque)malloc(sizeof(struct DequeRecord));
PtrToNode s = (PtrToNode)malloc(sizeof(struct Node));
s->Last = NULL;
s->Next = NULL;
dq->Front = s;
dq->Rear = s;
return dq;
}
int Push(ElementType X, Deque D) //头部插入
{
PtrToNode s = (PtrToNode)malloc(sizeof(struct Node));
s->Element = X;
if (D->Front == D->Rear) //空的情况
{
s->Next = NULL;
s->Last = D->Front;
D->Front->Next = s;
D->Rear = s;
}
else
{
s->Next = D->Front->Next;
s->Last = D->Front;
D->Front->Next = s;
s->Next->Last = s;
}
return 1;
}
ElementType Pop(Deque D)
{
if (D->Front == D->Rear)
return ERROR;
PtrToNode s = D->Front->Next;
int x = s->Element;
if (D->Front->Next == D->Rear) //只有一个元素
{
D->Front->Next = NULL;
D->Rear = D->Front;
}
else
{
D->Front->Next = s->Next;
s->Next->Last = D->Front;
}
free(s);
return x;
}
int Inject(ElementType X, Deque D)
{
PtrToNode s = (PtrToNode)malloc(sizeof(struct Node));
s->Element = X;
s->Last = D->Rear;
s->Next = NULL;
D->Rear->Next = s;
D->Rear = s;
return 1;
}
ElementType Eject(Deque D)
{
if (D->Front == D->Rear)
return ERROR;
PtrToNode s = D->Rear;
s->Last->Next = NULL;
D->Rear = s->Last;
int x = s->Element;
free(s);
return x;
}