首页 新闻 会员 周边

关于c语言链队列的一些问题

0
悬赏园豆:10 [待解决问题]
#include<stdio.h>
#include<stdlib.h>
typedef struct LinkQueue {
    int data;
    struct LinkQueue *next;
}LinkQueue,*LinkStr;
typedef struct{
    LinkStr front;
    LinkStr rear;
}Link;
void InitQueue(Link* q);
void EnterQueue(Link* q);
void DeleteQueue(Link* q);
int main(void) {
    Link queue;
    InitQueue(&queue);
     EnterQueue(&queue);
     DeleteQueue(&queue);
     //disqueue(queue);
    return 0;
}
//初始化
void  InitQueue(Link* q) {
    q->front=q->rear=(LinkStr)malloc(sizeof(LinkQueue));
    if ((q->front = q->rear) == NULL) {
        printf("分配不成功");
    }
    else {
        q->front->next=q->rear->next= NULL;
    }
}
//入队列
void EnterQueue(Link* q) {
    LinkQueue *number = (LinkQueue*)malloc(sizeof(LinkQueue));
    if (number != NULL) {
        int n = 0;
        int flag = 1;
        printf("请输入元素:");
        while (flag) {
            scanf("%d", &n);
            if (n != -1) {
                number->data = n;
                number->next = NULL;
                q->rear->next = number;
                q->rear = number;
            }
            else {
                flag = 0;
            }
        }
    }
    
}
//出队列
void DeleteQueue(Link* q) {
    int* x;
    x = (int*)malloc(sizeof(int));
        if (q->front == q->rear) {
            printf("该队列为空队列");
        }
        LinkQueue *p;
        p = (LinkQueue*)malloc(sizeof(LinkQueue));
        p = q->front->next;
        *x = p->data;
        printf("出队列的元素为:%d", *x);
        q->front->next = p->next;
        if (q->rear == p) {
            q->rear = q->front;
        }
        free(p);
}

我输入1 3 2

出队的元素为何是2,不是1

新手小白。的主页 新手小白。 | 初学一级 | 园豆:75
提问于:2021-10-08 15:48
< >
分享
所有回答(1)
0

顺序问题吧,rear和front某个位置写反了,至于具体的话我代码再具体看
可能是这里的问题?
p = q->front->next; //改为p=q->rear
*x = p->data;
printf("出队列的元素为:%d", *x);

计算机知识杂谈 | 园豆:470 (菜鸟二级) | 2021-10-09 21:12

还是不行

支持(0) 反对(0) 新手小白。 | 园豆:75 (初学一级) | 2021-10-10 21:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册