tiaya01 2021-11-18 20:44 采纳率: 88.2%
浏览 16
已结题

为什么输出结果有问题啊?实在是看不出来

img

#include<iostream>
using namespace std;

typedef struct{
    int id;
    char name[10];
}QElemtype;
typedef struct qNode{
    QElemtype data;
    struct qNode *next;
}qNode,*QueuePtr;
typedef struct{
    qNode *front;
    qNode *rear;
}LinkQueue;

void InitQueue(LinkQueue &Q);
void EnQueue(LinkQueue &Q,QElemtype e);
void DeQueue(LinkQueue &Q);
int QueueEmpty(LinkQueue &Q);
void OutQueue(LinkQueue Q);

int main(){
    LinkQueue s;
    QElemtype newdata;
    InitQueue(s);
    int n;
    cout << "请输入链队的长度:" << endl;
    cin >> n;
    for(int i=1;i<=n;i++){
        cout << "输入链队的第" << i << "个元素" << endl;
        cin >> newdata.id >> newdata.name;
        EnQueue(s,newdata);
    }
    //cout << "请输出元素:" << endl;
    OutQueue(s);
    while (!QueueEmpty(s))
    {
        DeQueue(s);
    }
    
    return 0;
}
//链队的初始化
void InitQueue(LinkQueue &Q){
    Q.front=Q.rear=new qNode;
    Q.front->next=NULL;
} 
//入队
void EnQueue(LinkQueue &Q,QElemtype e){
    QueuePtr p;
    p=new qNode;
    p->data=e;
    p->next=NULL;
    Q.rear=p;
}
//出队
void DeQueue(LinkQueue &Q){
    QueuePtr p;
    if(Q.front==Q.rear)
    exit(1);
    p=Q.front->next;
    QElemtype &e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
    Q.rear=Q.front;
    delete(p);
} 
//判断栈是否为空
int QueueEmpty(LinkQueue &Q){
    if (Q.front==Q.rear)
        return 1;
    else
        return 0;
}
//遍历输出
void OutQueue(LinkQueue Q){
    qNode *s;
    s=Q.front->next;
    if(!s)
    cout << "is empty!" << endl;
    else
    cout << "元素输出:"<< endl;
    while(s){
        printf("%d",s->data);
        s=s->next;
    }
    cout << endl;
} 

想输出结果,结果就是出不来……

  • 写回答

1条回答 默认 最新

  • mzhan017 2021-11-19 20:03
    关注

    enque没有用前一个元素的next

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 11月18日

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波