【数据结构】PTA网站上的一个双端队列题

胖太_2007 2015-10-05 09:22:59
在下学生,在做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是题目中给出的,结果是这样:

请帮我看看段错误在哪,谢谢!
...全文
807 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2015-10-06
  • 打赏
  • 举报
回复
因为 某些编译器,会自动加上返回值, 或者不认为无返回值,是个错误. 最多给个警告,而警告很多时候会被忽略
胖太_2007 2015-10-06
  • 打赏
  • 举报
回复
引用 3 楼 lm_whales 的回复:
1)函数 Deque CreateDeque() 没有返回任何值,显然写错了
大哥真乃神人也!!加了return D;以后直接满分啊!!感激涕零! 现在我唯一的疑问是:为何我没return在自家编译器上还过还能运行了呢?而且没有任何报错呢? 感觉离结贴进了。
胖太_2007 2015-10-06
  • 打赏
  • 举报
回复
从能够运行来看,是它自己加上返回值了,然而都不报告一下,害我纠结了一下午。
lm_whales 2015-10-05
  • 打赏
  • 举报
回复
1)函数 Deque CreateDeque() 没有返回任何值,显然写错了
lm_whales 2015-10-05
  • 打赏
  • 举报
回复
这个双端队列分明就是个双向链表啊。 而且持有首尾两个节点。 初始化为首尾皆指向空的头结点。 第一个节点。两个指针皆空。 前后插入数据方式一样,所不同的就是指针处理方式不同
胖太_2007 2015-10-05
  • 打赏
  • 举报
回复
交上去是段错误。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧