鱼C论坛

 找回密码
 立即注册
查看: 3119|回复: 4

[已解决]小甲鱼数据结构与算法中的队列中的问题

[复制链接]
发表于 2017-7-4 14:59:32 | 显示全部楼层 |阅读模式
2鱼币
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef char ElemType;
  4. typedef struct QNode{
  5.         ElemType date;
  6.         struct QNode* next;
  7. }QNode,*QueuePrt;
  8. typedef struct{
  9.         QueuePrt front,rear;
  10. }LinkQueue;
  11. void InitQueue(LinkQueue *q)
  12. {
  13.         q->front=(QueuePrt)malloc(sizeof(QNode));
  14.         if(!q->front)
  15.         exit(0);
  16.         q->front=NULL;
  17.         q->rear=q->front;
  18. }
  19. void InsetQueue(LinkQueue *q,ElemType c)
  20. {
  21.         QueuePrt p;
  22.         p=(QueuePrt)malloc(sizeof(QNode));
  23.         if(!p)
  24.         return;
  25.         p->date=c;
  26.         p->next=NULL;
  27.         q->rear->next=p;
  28.         q->rear=p;       
  29. }
  30. void DelteQueue(LinkQueue *q)
  31. {
  32.         QueuePrt p;
  33.         if(q->front==q->rear)
  34.         return;
  35.         p=q->front;
  36.         q->front=q->front->next;
  37.         if(p==q->rear)
  38.         {
  39.                 q->rear=q->front;
  40.         }
  41.         free(p);
  42. }
  43. void DestoryQueue(LinkQueue *q)
  44. {
  45.         while(q->front)
  46.         {
  47.                 q->rear=q->front;
  48.                 q->front=q->front->next;
  49.                 free(q->rear);
  50.         }
  51. }
  52. void DisplayQueue(LinkQueue *q)
  53. {
  54.         while(q->front)
  55.         {
  56.                 printf("%c ",q->front->next->date);
  57.                 q->front++;
  58.         }
  59. }
  60. int main()
  61. {
  62.         LinkQueue q;
  63.         ElemType c;
  64.         InitQueue(&q);
  65.         printf("请输入进入队列的数:\n");
  66.         scanf("%c",&c);
  67.         while(c!='#')
  68.         {
  69.                 InsetQueue(&q,c);
  70.                 scanf("%c",&c);
  71.         }
  72.         DisplayQueue(&q);
  73.         DestoryQueue(&q);
  74.         return 0;       
  75. }
复制代码
最佳答案
2017-7-4 14:59:33
第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了

最佳答案

查看完整内容

第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-4 14:59:33 | 显示全部楼层    本楼为最佳答案   
第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-4 15:02:07 | 显示全部楼层
经过调试出现Program stopped at 0x401375 错误提示框
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-4 22:47:49 | 显示全部楼层
zlj19931010 发表于 2017-7-4 16:26
第一次插入的时候 q->rear 为NULL,在 q->rear->next 就报错了

谢谢解答,我是新手,请多多关照!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-5 08:09:20 | 显示全部楼层
看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 08:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表