数据结构的“链队列”,编译出来了,但运行时却出问题了??

hua_zhixing_ 2009-01-11 10:54:19
这样的程序应该不用多注释了吧。高手帮我调试一下。应该是逻辑错误,运行之后提示:“链队列.exe.已停止工作”。帮忙解决一下。先谢了!
//链队列
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef int status;

typedef struct qnode
{
int data;
struct qnode *next;
}linkqlist;

typedef struct
{
linkqlist *front;
linkqlist *rear;
}linkqueue;

status init(linkqueue *p)//构造空队列
{
p->rear=p->front=(linkqlist *)malloc(sizeof(linkqlist));
if(!p->front) exit(0);
p->front->next=NULL;
return 0;
}

status destory(linkqueue *q)//销毁队列
{
while(q->front)
{
q->rear=q->front->next;
free(q->front);
q->front=q->rear;
}
return 0;
}

status empty(linkqueue *q)//判断队列是否为空
{
int v;
if(q->front==q->rear) v=1;
else v=0;
return v;
}

status gethead(linkqueue *q)//返回队头元素
{
int v;
if(q->front==q->rear) v=-1;
else v=q->front->next->data;
return v;
}

status enqueue(linkqueue *q,int e)//插入元素
{
q->rear->next=(linkqlist *)malloc(sizeof(linkqlist));
q->rear=q->rear->next;
if(!q->rear) return -1;///????
q->rear->data=e;
q->rear->next=NULL;
return 0;
}

status dequeue(linkqueue *q)//删除头元素
{
linkqlist *p;
int e;
if(q->front==q->rear)
printf("the linklist is overflow");
else p=q->front->next;
q->front->next=p->next;
e=p->data;
if(q->rear==p) q->rear=q->front;
free(p);

return e;
}

status visit(linkqueue *p)//输出队列
{
linkqlist *q;
q=p->front->next;
printf("display the linklist:\n");
if(p->front==p->rear)
printf("the linklist is empty!");
else
{
while(q)
{ printf("%d->",q->data); q=q->next; }
printf("\n");
}
return 0;
}

int main(linkqueue *head)//主函数
{
int n,i;
int select;
int a,x1,x3,x5,e;
printf("create a empty linkqlist\n");
init(head);
printf("please input linkqlist length:\n");
scanf_s("%d",&n);
for(i=1;i<=n;i++)
{
printf("please input linkqlist value\n");
scanf_s("%d",&a);
enqueue(head,a);
}
visit(head);
printf("select 1======destory()\n");
printf("select 2======empty()\n");
printf("select 3======gethead()\n");
printf("select 4======enqueue()\n");
printf("select 5======dequeue()\n");
printf("please select ( 1====5 ):");
scanf_s("%d",&select);
switch(select)
{
case 1:
{
destory(head);
visit(head);
break;}
case 2:
{
x1=empty(head);
if(x1==1) printf("the linklist is empty!");
else printf("the linklist is full");
break;}
case 3:
{
x3=gethead(head);
printf("head->front->%d\n",x3);
break;}
case 4:
{
printf("please inster the value:");
scanf_s("%d",&e);
enqueue(head,e);
visit(head);
break;}
case 5:
{
x5=dequeue(head);
printf("x5",x5);
visit(head);
break;}
}
return 0;
}
...全文
119 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tangshuiling 的回复:]
xiaoyisnail 一会儿指这,一会儿指那...太搞笑了!
指针吗?就应该忽左忽右,忽东忽西的....O(∩_∩)O哈哈~
[/Quote]

很明显这位同学没看懂我说的是什么,我指的是代码中的front起的作用,一会儿作为真正的首元素的指针,一会儿变成了一个起标志作用的“伪头指针”,不知道你能不能理解。。。
brookmill 2009-01-12
  • 打赏
  • 举报
回复
楼主学习的态度还是不错的,值得鼓励

不过感觉楼主的基本功还不是很扎实,这个程序对楼主来说稍微复杂了一点
建议楼主先写一些最简单的单链表的代码,如果能把单链表彻底弄明白了,其它的复杂的结构学起来就很快了。
这个代码太长,问题多,又没排版,实在没有耐心细看,比自己写代码可累多了

case后面一般是不加大括号的,缩进对齐了就好了
“}”一定要单独占一行,不要和break挤在一起
代码之间适当多留一些空行、空格,注意可读性
tangshuiling 2009-01-11
  • 打赏
  • 举报
回复
xiaoyisnail 一会儿指这,一会儿指那...太搞笑了!
指针吗?就应该忽左忽右,忽东忽西的....O(∩_∩)O哈哈~
xiaoyisnail 2009-01-11
  • 打赏
  • 举报
回复
大致看了一遍,几乎每个函数里都有错,front和rear的控制上很有问题,一会儿front指向第一元素,一会儿front又变成一个指示头,front->next才指向第一个元素。。。没精力一个个去改了,楼主自己再仔细看看吧
brookmill 2009-01-11
  • 打赏
  • 举报
回复
调用init的时候,head根本就没初始化,init里面拿来就用,当然不行了
xiaoyisnail 2009-01-11
  • 打赏
  • 举报
回复
问题很多啊。。。
int main(linkqueue *head)这样就不对了,不能这样传参数给main的

可以改成:

int main()//主函数
{
linkqueue* head = (linkqueue*)malloc(sizeof(linkqueue));
int n,i;
int select;
int a,x1,x3,x5,e;
printf("create a empty linkqlist\n");
init(head);
...
brookmill 2009-01-11
  • 打赏
  • 举报
回复
这个又是怎么回事?
printf("x5",x5);
楼主是想写这个吧printf("%d",x5);
brookmill 2009-01-11
  • 打赏
  • 举报
回复
为什么要这么写?
int main(linkqueue *head)//主函数

正常的写法不行么
int main()
{
linkqueue *head;
......
tangshuiling 2009-01-11
  • 打赏
  • 举报
回复
没具体看,但下面的代码肯定是错的
]
status dequeue(linkqueue *q)//删除头元素
{
linkqlist *p;
int e;
if(q->front==q->rear)
printf("the linklist is overflow");
else p=q->front->next;
//这里应该加上free(q->front);否则头节点的空间没有释放
q->front->next=p->next;
e=p->data;
if(q->rear==p) q->rear=q->front;
free(p); //这一行应该写到p节点构造之后!而且应该是free(q->front); p既然等于头节点的下个节点就不应该被释放掉!

return e;
}

int main(linkqueue *head)//主函数的这种用法还是很少见的

69,371

社区成员

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

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