小jy2333 发表于 2020-11-6 21:54:43

求助大佬关于链式队列插入问题

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

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

typedef char datatype;
typedef struct node1 {
        datatype data;
        struct node1 *next;
} linkstack;
linkstack *hs;

typedef struct node2 {
        datatype data;
        struct node2 *next;
} linkqueue;

typedef struct {
        linkqueue *front,*rear;
} lqueue;
lqueue *hq;


void push(datatype x) {
        linkstack *s;
        s=malloc(sizeof(linkstack));
        s->data=x;
        s->next=hs;
        hs=s;
}

void pop() {
        linkstack *p;
        if(hs==NULL)
                printf("栈空");
        else {
                p=hs;
                hs=hs->next;
        }
}

void insertq(datatype x) {
        linkqueue *p;
        p=malloc(sizeof(linkqueue));
        p->data=x;
        p->next=NULL;
        if(hq->rear==NULL) {
                hq->front=p;
                hq->rear=p;
        } else {
                hq->rear->next=p;
                hq->rear=p;
        }
}

void deleteq() {
        linkqueue *p;
        if(hq->front==NULL)
                printf("队空");
        else {
                p=hq->front;
                hq->front=p->next;
                if(hq->front==NULL)
                        hq->rear=hq->front;
        }
}

void print1(head)
linkstack *head;
{
        linkstack *p;
        p=head;
        while(p!=NULL) {
                printf("%c      ",p->data);
                p=p->next;
        }
        printf("\n");
}

int main() {
        char str[]="abcdefrx";
        char m='a';
        int i=0;
        while(*(str+i)) {
                push(str);
                i++;
        }
        print1(hs);
        pop(hs);
        print1(hs);

        insertq(m);

}

运行结果经过测试发现可能就是错在insertq,不明白错在哪里,求好心人帮忙解答一下

shooan 发表于 2020-11-7 09:59:35

已经改好了,详细说明见注释
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef char datatype;
typedef struct node1 {
      datatype data;
      struct node1 *next;
} linkstack;
linkstack *hs;

typedef struct node2 {
      datatype data;
      struct node2 *next;
} linkqueue;

typedef struct {
      linkqueue *front,*rear;
} lqueue;
lqueue *hq;

void push(datatype x) {
      linkstack *s;
      s=malloc(sizeof(linkstack));
      s->data=x;
      s->next=hs;
      hs=s;
}


void pop() {
      linkstack *p;
      if(hs==NULL)
                printf("栈空");
      else {
                p=hs;
                hs=hs->next;
      }
}


void insertq(datatype x) {
      linkqueue *p;
      p=malloc(sizeof(linkqueue));
      p->data=x;
      p->next=NULL;
      
      // ============= 改变的东西
      // 原因,你的hq一开始只是一个指向NULL的指针,所以到56行就会出错,因为 不能对NULL访问rear。所以这里给hq分配空间
      if (hq == NULL) {
                                hq = malloc(sizeof(lqueue));
                                hq->rear = hq->front = NULL;
      }
      // ============= 结束
      
      if(hq->rear==NULL) {
                hq->front=p;
                hq->rear=p;
      } else {
                hq->rear->next=p;
                hq->rear=p;
      }
}


// 队列删除元素
void deleteq() {
      linkqueue *p;
      if(hq->front==NULL)
                printf("队空");
      else {
                p=hq->front;
                hq->front=p->next;
                if(hq->front==NULL)
                        hq->rear=hq->front;
      }
}

void print1(head)
linkstack *head;
{
      linkstack *p;
      p=head;
      while(p!=NULL) {
                printf("%c      ",p->data);
                p=p->next;
      }
      printf("\n");
}

int main() {
      char str[]="abcdefrx";
      char m='a';
      int i=0;
      
      linkqueue* p;                // 输出用
      
      while(*(str+i)) {
                push(str);
                i++;
      }
      print1(hs);
      pop(hs);
      print1(hs);

      insertq(m);
      
      // 输出测试
      p = hq->rear;
      printf("%c",p->data);
      

}
https://i.bmp.ovh/imgs/2020/11/a1f831b56fc9f8f6.png
页: [1]
查看完整版本: 求助大佬关于链式队列插入问题