守望星辰@ 2020-09-05 23:02 采纳率: 50%
浏览 99
已结题

数据结构中的队列问题

为什么程序运行到Insert()函数中Q->rear->next = node;这一行时会报错

运行结果显示next不能存放node的地址

#include <stdio.h>
#include <stdlib.h>
typedef struct QueueNode
{
    int  Data;
    QueueNode *next;
}QueueNode;

typedef struct QueueLink
{
    QueueNode *front;
    QueueNode *rear;
    int length;
}QueueLink;

void Init(QueueLink *Q);
void Insert(QueueLink *Q);
void Delete(QueueLink *Q);
void Print(QueueLink *Q);

int main()
{
    QueueLink q;
    int i;
    printf("1. 初始化\n");
    printf("2. 入队\n");
    printf("3. 出队\n");
    printf("4. 输出\n");
    printf("请输入选项:");
    while (scanf("%d",&i)==1)
    {
        switch (i)
        {
           case 1: Init(&q);
                   printf("\n");
                   break;

           case 2: Insert(&q);
                   printf("\n");
                   break;

           case 3: Delete(&q);
                   printf("\n");
                   break;

           case 4: Print(&q);
                   printf("\n");
                   break;

           default: printf("无该选项!!!\n");
                    break;
        }
        printf("1. 初始化\n");
        printf("2. 入队\n");
        printf("3. 出队\n");
        printf("4. 输出\n");
        printf("请输入选项:");
    }
    getchar();
    getchar();
    return 0;
}

void Init(QueueLink *Q)
{    
    QueueNode *head;
    head = (QueueNode *)malloc(sizeof(QueueNode));   //head为头结点,最开始时front和rear均指向它
    Q = (QueueLink *)malloc(sizeof(QueueLink));
    Q->front = Q->rear = head;
    Q->length = 0;
    printf("初始化成功!!!\n");
}

void Insert(QueueLink *Q)
{
    int data;
    QueueNode *node;
    node = (QueueNode *)malloc(sizeof(QueueNode));
    printf("请向结点中输入数据:");
    scanf("%d",&data);
    node->Data = data;
    node->next = NULL;
    Q->rear->next = node;
    Q->rear = node;
    //Q->length++;
    printf("入队成功!!!\n");
}

void Delete(QueueLink *Q)
{
    int data;
    QueueNode *p;
    if (Q->front == Q->rear)
    {
        printf("队列为空,无可出队元素!!!\n");
    }
    else
    {
        p = Q->front->next;         //将“要出队的元素”的地址暂存在p中
        Q->front->next = p->next;   //front的next域中存放“要出队元素”的下一个元素的地址

        if ((Q->rear) == p)         //如果rear指向被删元素,说明“被删元素”是最后一个元素,此时就应让rear指向头结点
        {
            Q->front = Q->rear;
        }
        free(p);
    }   
}

void Print(QueueLink *Q)
{
    if (Q->front == Q->rear)
    {
        printf("队列为空,无可出队元素!!!\n");
    }
    else
    {
        QueueNode *p=Q->front->next;
        while (p!=NULL)
        {
            printf("%d ",p->Data);
            p = p->next;
        }
        printf("\n");
    }
}
  • 写回答

2条回答 默认 最新

  • 兰舟千帆 Java领域优质创作者 2020-09-06 09:28
    关注

    你这个错误太多了,不只是你说的运行到你那里就会出错。我给你看了一下,一共四十一个错误,你信不信。哈哈哈。typedef struct QueueNode
    {
    int Data;
    QueueNode *next;//首先这里的语法是不对的,你的QueueNode前面要加struct的
    }QueueNode;还有其他的你自己再看看。这样的代码在vs里错误太多了,不信你自己试试。祝你学好编程,加油。

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

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波