鱼C论坛

 找回密码
 立即注册
查看: 3225|回复: 1

数据结构-队列

[复制链接]
发表于 2019-9-25 22:50:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef char ElemType;


  4. typedef struct QNode
  5. {
  6.         ElemType data;
  7.         struct QNode *next;
  8. }QNode,*QueuePrt;

  9. typedef struct{
  10.         QueuePrt front,rear;
  11. }LinkQueue;

  12. initQueue(LinkQueue *q)
  13. {
  14.         q->front=q->rear=(QueuePrt)malloc(sizeof(QNode));
  15.         if(!q->front)
  16.                 exit(0);
  17.         q->front->next=NULL;
  18. }

  19. InsertQueue(LinkQueue *q,ElemType e)
  20. {
  21.         QueuePrt p;
  22.         p=(QueuePrt)malloc(sizeof(QNode));
  23.         if(p==NULL)
  24.                 exit(0);
  25.         p->data=e;
  26.         p->next=NULL;
  27.         q->rear->next=p;
  28.         q->rear=p;
  29. }

  30. DeleteQueue(LinkQueue *q, ElemType *e)
  31. {
  32.         QueuePrt p;
  33.         if(q->front==q->rear)
  34.                 return;
  35.         p=q->front->next;
  36.         *e=p->data;
  37.         q->front->next=p->next;
  38.         if(q->rear==p)
  39.                 q->rear=q->front;
  40.         free(p);
  41. }

  42. DestroyQueue(LinkQueue *q)
  43. {
  44.         while(q->front)
  45.         {
  46.                 q->rear=q->front->next;
  47.                 free(q->front);
  48.                 q->front=q->rear;
  49.         }
  50. }

  51. int main()
  52. {
  53.        
  54.         QueuePrt p;
  55.         LinkQueue q;
  56.         ElemType e;
  57.         initQueue(&q);
  58.        

  59.        
  60.         printf("请输入字符串,以#作为结束:\n");
  61.         scanf("%c",&e);

  62.         while(e!='#')
  63.         {
  64.                 InsertQueue(&q,e);
  65.                 scanf("%c",&e);
  66.         }
  67.         p=q.front->next;
  68.         while(p)
  69.         {
  70.                 printf("%c",p->data);
  71.                 p=p->next;

  72.         }
  73. }








复制代码


main函数里ElemType e;为什么这句一定要在initQueue(&q);前面,否则就会报错。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-26 09:51:42 | 显示全部楼层
我的没有报错啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 22:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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