#6-7 Deque (25分)
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.
#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 */
Deque CreateDeque()
{
Deque q=(Deque)malloc(sizeof(struct DequeRecord));
q->Front=(PtrToNode)malloc(sizeof(struct Node));
q->Front->Next=NULL;
q->Rear=q->Front;
q->Rear->Next=NULL;
return q;
}
int Push( ElementType X, Deque D )
{
PtrToNode temp;
temp=(PtrToNode)malloc(sizeof(struct Node));
if(!temp) return 0;
temp->Element=X;
if(D->Front==D->Rear)
{
D->Front->Next=temp;
temp->Last=D->Front;
temp->Next=NULL;
D->Rear=temp;
return 1;
}
temp->Next=D->Front->Next;
temp->Last=D->Front;
D->Front->Next->Last=temp;
D->Front->Next=temp;
return 1;
}
ElementType Pop( Deque D )
{
if(D->Front==D->Rear)
return ERROR;
int temp=D->Front->Next->Element;
PtrToNode t=D->Front->Next;
if(D->Front->Next==D->Rear)
{
D->Rear=D->Front;
D->Rear->Next=NULL;
free(t);
return temp;
}
D->Front->Next->Next->Last=D->Front;
D->Front->Next=D->Front->Next->Next;
free(t);
return temp;
}
int Inject( ElementType X, Deque D )
{
PtrToNode temp=(PtrToNode)malloc(sizeof(struct Node));
if(!temp) return 0;
temp->Element=X;
if(D->Front==D->Rear)
{
D->Front->Next=temp;
temp->Last=D->Front;
D->Rear=temp;
return 1;
}
D->Rear->Next=temp;
temp->Last=D->Rear;
temp->Next=NULL;
D->Rear=temp;
return 1 ;
}
ElementType Eject( Deque D )
{
if(D->Front==D->Rear)
return ERROR;
int temp = D->Rear->Element;
PtrToNode t = D->Rear;
D->Rear = D->Rear->Last;
D->Rear->Next=NULL;
free(t);
return temp;
}