代码已跑通,完美运行!
Push(X,D):将项X插入到双端队列D的前端;
Pop(D):从双端队列D中删除前端项并将其返回;
Inject(X,D):将项X插入到双端队列D的尾端。
Enject(D):从双端队列D中删除尾端项并将其返回。
#define _CRT_SECURE_NO_WARNINGS
#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);
void Error(char *s);
void FatalError(char *s);
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;
}
}
getchar();
return 0;
}
/* Your function will be put here */
Deque CreateDeque()
{
Deque D;
PtrToNode head;
D = (Deque)malloc(sizeof(struct DequeRecord));
head = (PtrToNode)malloc(sizeof(struct Node));
if (D&&head)
{
head->Last = NULL;
head->Next = NULL;
D->Front = head;
D->Rear = head;
}
else
{
FatalError("out of space!!!");
}
return D;
}
int Push(ElementType X, Deque D)
{
PtrToNode p;
p = (PtrToNode)malloc(sizeof(struct Node));
if (!p)
{
FatalError("out of space!!!");
return 0;
}
p->Element = X;
p->Last = D->Front;
// 空队列情况
if (D->Front == D->Rear)
{
D->Rear = p;
p->Next = NULL;
}
// 一般情况
else
{
p->Next = D->Front->Next;
D->Front->Next->Last = p;
}
D->Front->Next = p;
return 1;
}
ElementType Pop(Deque D)
{
PtrToNode p;
int X;
// 空队列
if (D->Front == D->Rear)
{
Error("empty deque!!!");
return ERROR;
}
p = D->Front->Next;
X = p->Element;
// 如果只有一个节点的情况
if (D->Front->Next == D->Rear)
{
D->Front->Next = NULL;
D->Rear = D->Front;
}
// 一般情况
else
{
p->Next->Last = D->Front;
D->Front->Next = p->Next;
}
free(p);
return X;
}
// 末尾添加一个元素
int Inject(ElementType X, Deque D)
{
PtrToNode p;
p = (PtrToNode)malloc(sizeof(struct Node));
if (!p)
{
FatalError("out of space!!!");
return 0;
}
p->Element = X;
// 空队列
if (D->Front == D->Rear)
{
D->Front->Next = p;
p->Last = D->Front;
D->Rear = p;
return 1;
}
D->Rear->Next = p;
p->Next = NULL;
p->Last = D->Rear;
D->Rear = p;
return 1;
}
// 从末尾移除一个元素
ElementType Eject(Deque D)
{
PtrToNode p;
int X;
if (D->Front == D->Rear)
{
Error("empty deque!!!");
return ERROR;
}
p = D->Rear;
X = p->Element;
D->Rear = p->Last;
D->Rear->Next = NULL;
free(p);
return X;
}
void Error(char *s) {
printf("Error:%s\n", s);
}
void FatalError(char *s) {
printf("FatalError:%s\n", s);
}
Operation GetOp()
{
char opt[7];
scanf_s("%s", opt, 7);
if (strcmp(opt, "push") == 0)
return push;
else if (strcmp(opt, "pop") == 0)
return pop;
else if (strcmp(opt, "inject") == 0)
return inject;
else if (strcmp(opt, "eject") == 0)
return eject;
else if (strcmp(opt, "end") == 0)
return end;
}
void PrintDeque(Deque D)
{
PtrToNode p;
printf("Inside deque: ");
p = D->Front->Next;
while (p != NULL)
{
printf("%d ", p->Element);
p = p->Next;
}
printf("\n---------\n");
}