在下学生,在做PTA上作业时候,有一个双端队列的问题,做了之后在自己电脑上能运行,可是提交上去说是段错误。
问题:
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.
我的代码:
[code=c][/#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;
}
}
return 0;
}
/* Your function will be put here */
Deque CreateDeque(){
Deque D;
PtrToNode head;
head = malloc( sizeof( struct Node ) );
D = malloc( sizeof( struct DequeRecord ) );
if( D&&head ){
head->Last = head->Next = NULL;
D->Front = D->Rear = head;
}
/*else
FatalError( "out of space!!!" );*/
}
int Push( ElementType X, Deque D ){
PtrToNode p;
p = malloc( sizeof( struct Node ) );
if( !p ){
/*FatalError( "out of space!!!" );*/
return 0;
}
p->Element = X;
p->Last = D->Front;
if( D->Rear == D->Front ){
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 ){
ElementType X;
PtrToNode p;
if ( D->Rear == D->Front ){
/*Error( "empty deque!!!" );*/
return ERROR;
}
p = D->Front->Next;
X = p->Element;
if ( !p->Next ){
D->Front->Next = NULL;
D->Rear = D->Front;
}
else{
p->Last->Next = p->Next;
p->Next->Last = p->Last;
}
free( p );
return X;
}
int Inject( ElementType X, Deque D ){
PtrToNode p;
p = malloc( sizeof( struct Node ) );
if( !p ){
/*FatalError( "out of space!!!" );*/
return 0;
}
p->Element = X;
p->Last = D->Rear;
p->Next = NULL;
D->Rear = D->Rear->Next = p;
return 1;
}
ElementType Eject( Deque D ){
ElementType X;
PtrToNode p;
if ( D->Rear == D->Front ){
/*Error( "empty deque!!!" );*/
return ERROR;
}
p = D->Rear;
X = p->Element;
p->Last->Next = NULL;
D->Rear = p->Last;
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 temp;
printf( "Inside deque: " );
temp = D->Front->Next;
while (temp != NULL )
{
printf("%d ", temp->Element);
temp = temp->Next;
}
printf("\n---------\n");
}
code]
其中main是题目中给出的,结果是这样:
请帮我看看段错误在哪,谢谢!