aaasdfgzzy 2022-10-25 20:18 采纳率: 79.4%
浏览 67
已结题

链式队列存储数据-数据结构-C语言

//------信息没有存入队列头中,队列尾正常存入,可以正常打印,队列头中没有元素。
//----部分代码
//-------队列的定义
typedef struct Qnode
{
    struct Qnode *next;
    char data;
} QNode,*QueuePtr;   //链队列存储结构

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;          //链队列指针
LinkQueue Q;          //队列全局变量Q

void InitQueue(LinkQueue *Q)    //初始化队列
{
    Q->front= (QueuePtr)malloc(sizeof(QNode));
    Q->rear = Q->front;
    Q->rear->next = NULL;
}
//------p指针指向一个内容,这个内容要存入队列Q中
void EnQueue(LinkQueue *Q,CSTree p)    //创建节点,将p->ch存入队列中
{
    QueuePtr q;
    q = (QueuePtr)malloc(sizeof(QNode));
    q->data = p->data;    // p指向的值插入队列中。
    q->next = NULL;
    if(Q->front == NULL) Q->front = Q->rear = q;
    else{
            Q->rear->next = q;
            Q->rear = q;
        }
    printf("Q->front->data = %c\n",Q->front->data);
}

img


Q->front->data 里的数据应该是 A ,但是打印出来的是 ?

//----全部代码

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int c = 0;

typedef struct CSTREE
{
    char data;
    struct CSTREE *firstchild,*nextchild;
} CSNode,*CSTree;   //二叉链表存储结构

typedef struct Qnode
{
    struct Qnode *next;
    char data;
} QNode,*QueuePtr;   //链队列存储结构

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;          //链队列指针
LinkQueue Q;          //队列全局变量Q

void GetTreeNode(char ch,CSTree *p)    // 创建一个结点,并把 ch 存入到其中,让 p 指向该结点并返回。
{
    *p = (CSTree)malloc(sizeof(CSNode));
    (*p)->data = ch;
    //printf("Gdata = %c\n",(*p)->data);
}

void InitQueue(LinkQueue *Q)    //初始化队列
{
    Q->front = (QueuePtr)malloc(sizeof(QNode));
    Q->rear = Q->front;
    Q->front->next = NULL;
}

void EnQueue(LinkQueue *Q,CSTree p)    //创建节点,将p->ch存入队列中
{
    QNode *q;
    q = (QueuePtr)malloc(sizeof(QNode));
    q->data = p->data;
    q->next = NULL;
    
    if(Q->front == NULL) 
    {
        Q->front = p;
        Q->rear = p;
    }
    else{
          Q->rear->next = q;
          Q->rear = q;
        }
    printf("front->data = %c\n",Q->front->data);
    printf("rear->data = %c\n",Q->rear->data);
}

void GetHead(LinkQueue *Q,CSTree *s)    //获取队列头的值
{
    (*s)->data = Q->front->data;
    printf("ch = %c\n",Q->front->data);
    printf("ch = %c\n",(*s)->data);
}

void DeQueue(LinkQueue *Q)    //删除队列头的值
{
    LinkQueue *q;
    q->front = Q->front->next;
    free(Q->front);    //释放空间
    Q->front = q->front;
}

void pre(CSTree *s,CSTree T)
{
    if(T->data != (*s)->data)    //用头指针寻找与 s->data 的值相等的结点。
    {
        pre(&s,T->firstchild);
        pre(&s,T->nextchild);
    }
    *s = T;
}

void Creat(CSTree *T)
{
    CSTree p,s,r;
    char fa,ch;
    scanf("%c %c",&fa,&ch);
    for(  ; ch != ' ' ; )    // fa、ch 为父子对
    {
        GetTreeNode(ch,&p);    //p指向该节点  
        EnQueue(&Q,p);         //把 p指向的值存入队列中
        if(fa == '#')
        {
            *T = p;    // T指向头节点,p用来寻找节点。     
        }
        else
        {
            GetHead(&Q,&s);        //获取队列头的值
            while(s->data != fa)//判断队列头的值是否与 父子对中的 fa 相等
            {
                DeQueue(&Q);        //删除队列头的值
                GetHead(&Q,&s);    //再次获取队列头的值
            }
            pre(&s,*T);        //寻找与队列头的值相等的结点,并用 s 指向它
            if(!(s->firstchild))    //如果 s结点的左孩子不空
            {
                s->firstchild = p; // p 指向的结点作为 s 的左孩子
                r = p;
            }
            else
            {
                r->nextchild = p; // p 作为 r 的右孩子
                r = p;
            }
        }
        scanf(" %c %c",&fa,&ch);
        printf("fa= %c\nch= %c\n",fa,ch);
    }
}

void Preorder(CSTree T)  //前序遍历二叉树
{
    if(T)
    {
        //printf("%c ",T->data);
        Preorder(T->firstchild);
        Preorder(T->nextchild);
    }
}

int main()
{
    CSTree T = NULL;
    InitQueue(&Q);
    printf("请输入父子对:\n");
    Creat(&T);
    printf("\n前序打印二叉树: ");
    Preorder(T);
    return 0;
}
    
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-10-25 21:55
    关注

    这段代码里,看不出问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月26日
  • 已采纳回答 10月26日
  • 修改了问题 10月26日
  • 修改了问题 10月26日
  • 展开全部

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装