m0_66051118 2023-04-12 20:07 采纳率: 76.3%
浏览 46
已结题

C语言链队列的数据结构问题


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node
{
    int data;
    struct node *next;
}Node;
typedef struct
{
    Node *front;
    Node *rear;
}lqueuetp;

void InitQueue(lqueuetp *lq)
{
    //设置一个空的链队列lq
    lq->front = (Node*)malloc(sizeof(Node));
    lq->front->next = NULL;
    lq->front = lq->rear;        //链队列空的判断条件 
}

void QueueEmpty(lqueuetp *lq)
{
    if(lq->front = lq->rear)
        printf("链队列为空");
    else
        printf("链队列不为空");    
}

int Head(lqueuetp *lq)        //读链队列队头元素 
{
    //若链队列lq不为空,则返回队头元素值,否则返回0
    if(lq->front == lq->rear)
        return 0;
    else
        return(lq->front->next->data);
}

void EnQueue(lqueuetp *lq, int x)
{
    //在链队列lq中,插入x为新的队尾元素
    Node *p;
    p = (Node*)malloc(sizeof(Node));
    p->data = x;
    p->next = NULL;
    lq->rear->next = p;        //尾插 
    lq->rear = p; 
}

int DelQueue(lqueuetp *lq)
{
    //若链队列lq不为空,则删去队头元素并返回元素值,否则返回0
    int x;
    Node *q;
    q = (Node*)malloc(sizeof(Node));
    if(lq->front == lq->rear)
        return 0;
    else
    {
        q = lq->front->next;
        lq->front->next = q->next;
        if(q->next == NULL)    
            lq->rear = lq->front;            //当链队列中仅有一个结点时,出队时还需修改尾指针
        x = q->data;
        free(q);
        return x; 
    } 
}

int main()
{
    int x,i,n;
    printf("链队列长度:");
    scanf("%d",&n);
    lqueuetp *lq;
    lq = (lqueuetp*)malloc(sizeof(lqueuetp));
    InitQueue(lq);
    printf("入队列:\n");
    for(i = 1; i <= n;i++)
    {
        scanf("%d",&x);
        EnQueue(lq,x);
    }
    QueueEmpty(lq);
    printf("读取队列对头元素:%d\n",Head(lq));
    printf("出队列:%d\n",DelQueue(lq));
    return 0;
}

这个代码哪里出现问题了?为什么入队列时不成功?

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-04-12 20:58
    关注

    修改如下,改动处见注释,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    typedef struct node
    {
        int    data;
        struct node *next;
    }Node;
    typedef struct
    {
        Node *front;//队头指针
        Node *rear; //队尾指针
    }lqueuetp;
    
    void InitQueue(lqueuetp *lq)
    {
        //设置一个空的链队列lq
        //lq->front = (Node*)malloc(sizeof(Node));  修改
        //lq->front->next = NULL;                   修改
        lq->front = lq->rear = NULL;  //链队列空的判断条件
    }
    
    void QueueEmpty(lqueuetp *lq)
    {
        if(!lq->front)  //if(lq->front = lq->rear) 修改
            printf("链队列为空\n");
        else
            printf("链队列不为空\n");
    }
    
    int Head(lqueuetp *lq)        //读链队列队头元素 
    {
        //若链队列lq不为空,则返回队头元素值,否则返回0
        if(!lq->front) //if(lq->front == lq->rear)         修改
            return 0;
        else
            return lq->front->data ; //(lq->front->next->data);修改
    }
    
    void EnQueue(lqueuetp *lq, int x)
    {
        //在链队列lq中,插入x为新的队尾元素
        Node *p;
        p = (Node*)malloc(sizeof(Node));
        p->data = x;
        p->next = NULL;
    
        if (!lq->front)     //修改
            lq->front = p;  //修改
        else                //修改
            lq->rear->next = p;//尾插
        lq->rear = p;       //修改
    }
    
    int DelQueue(lqueuetp *lq)
    {
        //若链队列lq不为空,则删去队头元素并返回元素值,否则返回0
        int x;
             //Node *q;                        //修改
             //q = (Node*)malloc(sizeof(Node));//修改
        if(!lq->front)  //if(lq->front == lq->rear)//修改
            return 0;
        //else                                //修改
        //{                                   //修改
        x = lq->front->data;
            //q = lq->front->next;            //修改
            //lq->front->next = q->next;      //修改
        if(lq->front->next == NULL){    //只剩一个结点
            free(lq->front);
            lq->front = lq->rear = NULL;
        }
        else{                          //多个结点
            Node *q = lq->front->next;
            free(lq->front);
            lq->front = q;
        }
            //lq->rear = lq->front;            //当链队列中仅有一个结点时,出队时还需修改尾指针
            //x = q->data;
            //free(q);
        return x;
        //}
    }
    
    int main()
    {
        int x,i,n;
        printf("链队列长度:");
        scanf("%d",&n);
        lqueuetp *lq;
        lq = (lqueuetp*)malloc(sizeof(lqueuetp));
        InitQueue(lq);
        printf("入队列:\n");
        for(i = 1; i <= n;i++)
        {
            scanf("%d",&x);
            EnQueue(lq,x);
        }
        QueueEmpty(lq);
    
        printf("读取队列对头元素:%d\n",Head(lq));
    
        x = DelQueue(lq);                        //修改
        printf(x ? "出队列:%d\n" : "队空\n",x); //修改
        x = DelQueue(lq);                        //修改
        printf(x ? "出队列:%d\n" : "队空\n",x); //修改
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月12日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”