weixin_63226529 2022-09-29 14:25 采纳率: 75%
浏览 42
已结题

队列的基本操作 插入删除等

问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图

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

#define MAXSIZE 1024
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int QElemType;

typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;

void InitQueue(LinkQueue&Q)//构造一个队列
{

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return;

}

void EnQueue(LinkQueue&Q,QElemType e)//插入元素e为Q的新队尾
{
QNode *p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NILL;
Q.rear->next=p;
Q.rear=p;
return;
}

void DeQueue(LinkQueue&Q,QElemType e)//删除队头元素
{
if(Q.front==Q.rear)return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
return;
}

void DestroyQueue(LinkQueue&Q)//销毁队列
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return;
}

int main()
{
int e;
LinkQueue Q;
printf("请输入您的元素:\n");
scanf("%d",&e);
EnQueue(Q,e);
DestroyQueue(Q);
return 0;
}

运行结果及报错内容

C:\Users\86182\Desktop\队列.cpp(37) : error C2065: 'NILL' : undeclared identifier
C:\Users\86182\Desktop\队列.cpp(37) : error C2440: '=' : cannot convert from 'int' to 'struct QNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(45) : error C2562: 'DeQueue' : 'void' function returning a value
C:\Users\86182\Desktop\队列.cpp(43) : see declaration of 'DeQueue'
C:\Users\86182\Desktop\队列.cpp(46) : error C2065: 'p' : undeclared identifier
C:\Users\86182\Desktop\队列.cpp(46) : error C2440: '=' : cannot convert from 'struct QNode *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(47) : error C2227: left of '->data' must point to class/struct/union
C:\Users\86182\Desktop\队列.cpp(48) : error C2227: left of '->next' must point to class/struct/union
C:\Users\86182\Desktop\队列.cpp(49) : error C2446: '==' : no conversion from 'int' to 'struct QNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\86182\Desktop\队列.cpp(49) : error C2040: '==' : 'struct QNode *' differs in levels of indirection from 'int'
执行 cl.exe 时出错.

队列.obj - 1 error(s), 0 warning(s)

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-09-30 16:45
    关注

    修改如下,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    #include <crtdbg.h>
    
    #define MAXSIZE 1024
    #define OVERFLOW -2
    #define OK 1
    #define ERROR 0
    typedef int QElemType;
    typedef struct QNode{
        QElemType data;
        struct QNode* next;
    }QNode, * QueuePtr;
    typedef struct
    {
        QueuePtr front;//队头指针
        QueuePtr rear; //队尾指针
    }LinkQueue;
    
    void InitQueue(LinkQueue& Q)//构造一个队列
    {
        Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
        if (!Q.front) exit(OVERFLOW);
        Q.front->next = Q.rear->next = NULL; //修改 
        return;
    }
    
    void EnQueue(LinkQueue& Q, QElemType e)//插入元素e为Q的新队尾
    {
        QNode* p;
        p = (QueuePtr)malloc(sizeof(QNode));
        if (!p) exit(OVERFLOW);
        p->data = e;
        p->next = NULL;         //NILL;
        if (!Q.front->next)     //修改
            Q.front->next = p;  //修改 
        else       
            Q.rear->next = p;  //修改 
        Q.rear = p;            //修改 
        return;
    }
    
    void DeQueue(LinkQueue& Q, QElemType &e)//删除队头元素 修改 QElemType e
    {
        if (!Q.front->next) { //Q.front->next == Q.rear  修改  
            e = -999;
            return;  // ERROR; 修改
        }
        QueuePtr p = Q.front->next; //修改 
        e = p->data;
        Q.front->next = p->next;
        //if (Q.rear == p) Q.rear = Q.front; //修改 
        free(p);
        return;
    }
    
    void DestroyQueue(LinkQueue& Q)//销毁队列
    {
        while (Q.front)
        {
            Q.rear = Q.front->next;
            free(Q.front);
            Q.front = Q.rear;
        }
        return;
    }
    
    int main()
    {
        int e;
        LinkQueue Q;
    
        InitQueue(Q);       //修改  
        //printf("请输入您的元素:\n"); //修改 
        //scanf("%d", &e);   //修改 
        EnQueue(Q, 8);
        EnQueue(Q, 10);
    
        DeQueue(Q, e);
        printf("%d\n", e);
    
        DeQueue(Q, e);
        printf("%d\n", e);
    
        DeQueue(Q, e);
        printf("%d\n", e);
    
        DestroyQueue(Q);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 创建了问题 9月29日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料