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!
");
break;
case pop:
X = Pop(D);
if ( X==ERROR ) printf("Deque is Empty!
");
break;
case inject:
scanf("%d", &X);
if (!Inject(X, D)) printf("Memory is Full!
");
break;
case eject:
X = Eject(D);
if ( X==ERROR ) printf("Deque is Empty!
");
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
代码:
#include <stdio.h> #include <stdlib.h> #include <string.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! "); break; case pop: X = Pop(D); if ( X==ERROR ) printf("Deque is Empty! "); break; case inject: scanf("%d", &X); if (!Inject(X, D)) printf("Memory is Full! "); break; case eject: X = Eject(D); if ( X==ERROR ) printf("Deque is Empty! "); break; case end: PrintDeque(D); done = 1; break; } PrintDeque(D); } return 0; } void PrintDeque( Deque D ) { if(D -> Rear == D -> Front) { printf("Deque is Empty!"); return; } printf("Inside Deque:"); PtrToNode p = D -> Front -> Next; while(p) { printf(" %d",p -> Element); p = p -> Next; } } Operation GetOp() { char s[10]; scanf("%s",s); if(!strcmp(s,"Pop"))return pop; else if(!strcmp(s,"Push"))return push; else if(!strcmp(s,"Inject"))return inject; else if(!strcmp(s,"Eject"))return eject; else return end; } Deque CreateDeque() { Deque q = (Deque)malloc(sizeof(struct DequeRecord));///申请个队列 PtrToNode p = (PtrToNode)malloc(sizeof(struct Node));///申请一个头结点,不添加数据 p -> Next = p -> Last = NULL;///头结点最初前后都指向空 q -> Front = q -> Rear = p;///队列首尾都指向它 return q; } int Push( ElementType X, Deque D )///在首插入一个结点 { PtrToNode p = (PtrToNode)malloc(sizeof(struct Node)); if(p == NULL)return 0;///申请失败 p -> Element = X;///添加数据 p -> Next = D -> Front -> Next;///p指向原来的第一个非头结点 if(p -> Next)p -> Next -> Last = p;///然后非头结点还要指回来 前提是存在 p -> Last = D -> Front; D -> Front -> Next = p; if(D -> Rear == D -> Front) D -> Rear = p; return 1;///成功 } int Inject( ElementType X, Deque D ) { PtrToNode p = (PtrToNode)malloc(sizeof(struct Node)); if(p == NULL)return 0;///申请失败 p -> Element = X;///添加数据 p -> Last = D -> Rear; p -> Next = NULL; D -> Rear -> Next = p; D -> Rear = p; return 1;///成功 } ElementType Pop( Deque D ) { if(D -> Front == D -> Rear)return ERROR;///只有头结点,即队列为空 PtrToNode p = D -> Front -> Next; ElementType d = p -> Element; D -> Front -> Next = p -> Next; if(p -> Next)p -> Next -> Last = D -> Front; free(p); if(D -> Front -> Next == NULL)D -> Rear = D -> Front; return d; } ElementType Eject( Deque D ) { if(D -> Front == D -> Rear)return ERROR; PtrToNode p = D -> Rear; ElementType d = p -> Element; D -> Rear = p -> Last; p -> Last -> Next = NULL; free(p); return d; }