鱼C论坛

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

[已解决]求助大佬关于链式队列插入问题

[复制链接]
发表于 2020-11-6 21:54:43 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 小jy2333 于 2020-11-6 23:11 编辑

这是题目要求:编程序判断一个字符序列是否是回文,要求采用链式队列和链式堆栈。
算法提示:设字符数组str中存放了要判断的字符串。把字符数组中的字符逐个分别存入队列和堆栈,然后逐个出队列和退栈并比较出队列的字符和退栈的字符是否相等,若全部相等则该字符序列是回文,否则就不是回文。
下面是我写的未完成的代码,到了insertq函数关于队列插入的地方卡主了
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>

  4. typedef char datatype;
  5. typedef struct node1 {
  6.         datatype data;
  7.         struct node1 *next;
  8. } linkstack;
  9. linkstack *hs;

  10. typedef struct node2 {
  11.         datatype data;
  12.         struct node2 *next;
  13. } linkqueue;

  14. typedef struct {
  15.         linkqueue *front,*rear;
  16. } lqueue;
  17. lqueue *hq;


  18. void push(datatype x) {
  19.         linkstack *s;
  20.         s=malloc(sizeof(linkstack));
  21.         s->data=x;
  22.         s->next=hs;
  23.         hs=s;
  24. }

  25. void pop() {
  26.         linkstack *p;
  27.         if(hs==NULL)
  28.                 printf("栈空");
  29.         else {
  30.                 p=hs;
  31.                 hs=hs->next;
  32.         }
  33. }

  34. void insertq(datatype x) {
  35.         linkqueue *p;
  36.         p=malloc(sizeof(linkqueue));
  37.         p->data=x;
  38.         p->next=NULL;
  39.         if(hq->rear==NULL) {
  40.                 hq->front=p;
  41.                 hq->rear=p;
  42.         } else {
  43.                 hq->rear->next=p;
  44.                 hq->rear=p;
  45.         }
  46. }

  47. void deleteq() {
  48.         linkqueue *p;
  49.         if(hq->front==NULL)
  50.                 printf("队空");
  51.         else {
  52.                 p=hq->front;
  53.                 hq->front=p->next;
  54.                 if(hq->front==NULL)
  55.                         hq->rear=hq->front;
  56.         }
  57. }

  58. void print1(head)
  59. linkstack *head;
  60. {
  61.         linkstack *p;
  62.         p=head;
  63.         while(p!=NULL) {
  64.                 printf("%c      ",p->data);
  65.                 p=p->next;
  66.         }
  67.         printf("\n");
  68. }

  69. int main() {
  70.         char str[]="abcdefrx";
  71.         char m='a';
  72.         int i=0;
  73.         while(*(str+i)) {
  74.                 push(str[i]);
  75.                 i++;
  76.         }
  77.         print1(hs);
  78.         pop(hs);
  79.         print1(hs);

  80.         insertq(m);

  81. }
复制代码


运行结果经过测试发现可能就是错在insertq,不明白错在哪里,求好心人帮忙解答一下
最佳答案
2020-11-7 09:59:35
已经改好了,详细说明见注释
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>

  4. typedef char datatype;
  5. typedef struct node1 {
  6.         datatype data;
  7.         struct node1 *next;
  8. } linkstack;
  9. linkstack *hs;

  10. typedef struct node2 {
  11.         datatype data;
  12.         struct node2 *next;
  13. } linkqueue;

  14. typedef struct {
  15.         linkqueue *front,*rear;
  16. } lqueue;
  17. lqueue *hq;

  18. void push(datatype x) {
  19.         linkstack *s;
  20.         s=malloc(sizeof(linkstack));
  21.         s->data=x;
  22.         s->next=hs;
  23.         hs=s;
  24. }


  25. void pop() {
  26.         linkstack *p;
  27.         if(hs==NULL)
  28.                 printf("栈空");
  29.         else {
  30.                 p=hs;
  31.                 hs=hs->next;
  32.         }
  33. }


  34. void insertq(datatype x) {
  35.         linkqueue *p;
  36.         p=malloc(sizeof(linkqueue));
  37.         p->data=x;
  38.         p->next=NULL;
  39.         
  40.         // ============= 改变的东西
  41.         // 原因,你的hq一开始只是一个指向NULL的指针,所以到56行就会出错,因为 不能对NULL访问rear。所以这里给hq分配空间
  42.         if (hq == NULL) {
  43.                                 hq = malloc(sizeof(lqueue));
  44.                                 hq->rear = hq->front = NULL;
  45.         }
  46.         // ============= 结束
  47.         
  48.         if(hq->rear==NULL) {
  49.                 hq->front=p;
  50.                 hq->rear=p;
  51.         } else {
  52.                 hq->rear->next=p;
  53.                 hq->rear=p;
  54.         }
  55. }


  56. // 队列删除元素
  57. void deleteq() {
  58.         linkqueue *p;
  59.         if(hq->front==NULL)
  60.                 printf("队空");
  61.         else {
  62.                 p=hq->front;
  63.                 hq->front=p->next;
  64.                 if(hq->front==NULL)
  65.                         hq->rear=hq->front;
  66.         }
  67. }

  68. void print1(head)
  69. linkstack *head;
  70. {
  71.         linkstack *p;
  72.         p=head;
  73.         while(p!=NULL) {
  74.                 printf("%c      ",p->data);
  75.                 p=p->next;
  76.         }
  77.         printf("\n");
  78. }

  79. int main() {
  80.         char str[]="abcdefrx";
  81.         char m='a';
  82.         int i=0;
  83.         
  84.         linkqueue* p;                // 输出用
  85.         
  86.         while(*(str+i)) {
  87.                 push(str[i]);
  88.                 i++;
  89.         }
  90.         print1(hs);
  91.         pop(hs);
  92.         print1(hs);

  93.         insertq(m);
  94.         
  95.         // 输出测试
  96.         p = hq->rear;
  97.         printf("%c",p->data);
  98.         

  99. }
复制代码


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-7 09:59:35 | 显示全部楼层    本楼为最佳答案   
已经改好了,详细说明见注释
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>

  4. typedef char datatype;
  5. typedef struct node1 {
  6.         datatype data;
  7.         struct node1 *next;
  8. } linkstack;
  9. linkstack *hs;

  10. typedef struct node2 {
  11.         datatype data;
  12.         struct node2 *next;
  13. } linkqueue;

  14. typedef struct {
  15.         linkqueue *front,*rear;
  16. } lqueue;
  17. lqueue *hq;

  18. void push(datatype x) {
  19.         linkstack *s;
  20.         s=malloc(sizeof(linkstack));
  21.         s->data=x;
  22.         s->next=hs;
  23.         hs=s;
  24. }


  25. void pop() {
  26.         linkstack *p;
  27.         if(hs==NULL)
  28.                 printf("栈空");
  29.         else {
  30.                 p=hs;
  31.                 hs=hs->next;
  32.         }
  33. }


  34. void insertq(datatype x) {
  35.         linkqueue *p;
  36.         p=malloc(sizeof(linkqueue));
  37.         p->data=x;
  38.         p->next=NULL;
  39.         
  40.         // ============= 改变的东西
  41.         // 原因,你的hq一开始只是一个指向NULL的指针,所以到56行就会出错,因为 不能对NULL访问rear。所以这里给hq分配空间
  42.         if (hq == NULL) {
  43.                                 hq = malloc(sizeof(lqueue));
  44.                                 hq->rear = hq->front = NULL;
  45.         }
  46.         // ============= 结束
  47.         
  48.         if(hq->rear==NULL) {
  49.                 hq->front=p;
  50.                 hq->rear=p;
  51.         } else {
  52.                 hq->rear->next=p;
  53.                 hq->rear=p;
  54.         }
  55. }


  56. // 队列删除元素
  57. void deleteq() {
  58.         linkqueue *p;
  59.         if(hq->front==NULL)
  60.                 printf("队空");
  61.         else {
  62.                 p=hq->front;
  63.                 hq->front=p->next;
  64.                 if(hq->front==NULL)
  65.                         hq->rear=hq->front;
  66.         }
  67. }

  68. void print1(head)
  69. linkstack *head;
  70. {
  71.         linkstack *p;
  72.         p=head;
  73.         while(p!=NULL) {
  74.                 printf("%c      ",p->data);
  75.                 p=p->next;
  76.         }
  77.         printf("\n");
  78. }

  79. int main() {
  80.         char str[]="abcdefrx";
  81.         char m='a';
  82.         int i=0;
  83.         
  84.         linkqueue* p;                // 输出用
  85.         
  86.         while(*(str+i)) {
  87.                 push(str[i]);
  88.                 i++;
  89.         }
  90.         print1(hs);
  91.         pop(hs);
  92.         print1(hs);

  93.         insertq(m);
  94.         
  95.         // 输出测试
  96.         p = hq->rear;
  97.         printf("%c",p->data);
  98.         

  99. }
复制代码


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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