关于##i队列的链式结构#的问题,如何解决?

队列的链式存储
大家看看我下面的代码哪里有问题

#include 
#include 

struct Node {
    int Data;
    Node *next;
};

struct QNode {
    struct Node *front;
    struct Node *rear;
};

struct QNode *CreatQueue() {
    struct QNode *PtrS = (struct QNode *)malloc(sizeof(struct QNode)); //队列链
    struct Node *Head = (struct Node *)malloc(sizeof(struct Node)); //链表头结点
    PtrS->front = Head; //头结点
    PtrS->rear = Head; //尾结点
    Head->next = NULL;
    return PtrS;
}

void EnQueue(struct QNode *PtrS, int Data) {
    struct Node *S = (struct Node *)malloc(sizeof(struct Node));
    S->Data = Data;
    S->next = NULL;
    printf("%d入队列\n", S->Data);
    S->next = PtrS->rear;//rear指向最后元素
    PtrS->rear = S;
}

int DeQueue(struct QNode *PtrS) {
    int Data;
    struct Node *S;
    if (PtrS->front == NULL) {
        printf("队列空!\n");
        return 0;
    }
    S = PtrS->front;
    if (PtrS->front == PtrS->rear) //只有一个元素
        PtrS->front = PtrS->rear = NULL;
    else
        PtrS->front = PtrS->front->next;
    Data = S->Data;
    free(S);
    printf("%d出队\n", Data);
    return 0;
}

void outputQNode(struct QNode *PtrS) {
    struct Node *pMove = PtrS->front->next;
    while (pMove != NULL) {
        printf("%d\n", pMove->Data);
        pMove = pMove->next;
    }
}

int main() {
    struct QNode *PtrS;
    int i;
    CreatQueue();
    for (i = 0; i < 10; i++) {
        EnQueue(PtrS, i);
    }
    for (i = 0; i < 10; i++) {
        DeQueue(PtrS);
    }
    return 0;
}

题能发一下吗

该回答引用ChatGPT
在您的代码中,存在以下几个问题:

1、在 main 函数中没有为指针 PtrS 分配内存空间,因此在调用 EnQueue 和 DeQueue 函数时,程序会访问未分配的内存,导致出现未知错误。

2、在 EnQueue 函数中,指针 S 的 next 成员变量应该指向 NULL 而不是 PtrS->rear,因为 PtrS->rear 是指向队尾元素的指针,而此时队尾元素的 next 成员变量应该是 NULL,表示队尾。

3、在 EnQueue 函数中,应该先将 PtrS->rear 的 next 成员变量指向 S,再将 PtrS->rear 指向 S,表示将新元素 S 加入队尾。这个问题可能是因为上述的错误 2 导致的。

4、在 DeQueue 函数中,需要判断 PtrS->front 是否为 NULL,而不是 PtrS->rear。

5、在 DeQueue 函数中,应该先将 S 的 next 成员变量指向 PtrS->front->next,再将 PtrS->front 指向 PtrS->front->next,最后释放 S 所占用的内存空间。

6、在 outputQNode 函数中,应该从 PtrS->front->next 开始遍历链表,因为 PtrS->front 只是一个头结点,并不是真正的队首元素。

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

struct Node {
    int Data;
    Node *next;
};

struct QNode {
    struct Node *front;
    struct Node *rear;
};

struct QNode *CreatQueue() {
    struct QNode *PtrS = (struct QNode *)malloc(sizeof(struct QNode)); //队列链
    struct Node *Head = (struct Node *)malloc(sizeof(struct Node)); //链表头结点
    PtrS->front = Head; //头结点
    PtrS->rear = Head; //尾结点
    Head->next = NULL;
    return PtrS;
}

void EnQueue(struct QNode *PtrS, int Data) {
    struct Node *S = (struct Node *)malloc(sizeof(struct Node));
    S->Data = Data;
    S->next = NULL;
    printf("%d入队列\n", S->Data);
    PtrS->rear->next = S;
    PtrS->rear = S;
}

int DeQueue(struct QNode *PtrS) {
    int Data;
    struct Node *S;
    if (PtrS->front->next == NULL) {
        printf("队列空!\n");
        return 0;
    }
    S = PtrS->front->next;
    if (PtrS->front->next == PtrS->rear) //只有一个元素
        PtrS->front = PtrS->rear;
    else
        PtrS->front->next = PtrS->front->next->next;
    Data = S->Data;
    free(S);
    printf("%d出队\n", Data);
    return 0;
}

void outputQNode(struct QNode *PtrS) {
    struct Node *pMove = PtrS->front->next;
    while (pMove != NULL) {
        printf("%d\n", pMove->Data);
        pMove = pMove->next;
    }
}

int main() {
    struct QNode *PtrS;
    int i;
    PtrS = CreatQueue();
    for (i = 0; i < 10; i++) {
        EnQueue(PtrS, i);
    }
    for (i = 0; i < 10; i++) {
        DeQueue(PtrS);
    }
    return 0;
}


改动处见注释,供参考:

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

struct QNode {
    struct Node *front;
    struct Node *rear;
};

struct QNode *CreatQueue() {
    struct QNode *PtrS = (struct QNode *)malloc(sizeof(struct QNode)); //队列链
    struct Node *Head = (struct Node *)malloc(sizeof(struct Node)); //链表头结点
    PtrS->front = Head; //头结点
    PtrS->rear = Head; //尾结点
    Head->next = NULL;
    return PtrS;
}

void EnQueue(struct QNode *PtrS, int Data) {
    struct Node *S = (struct Node *)malloc(sizeof(struct Node));
    S->Data = Data;
    S->next = NULL;
    printf("%d入队列\n", S->Data);
    PtrS->rear->next = S; //S->next = PtrS->rear;//rear指向最后元素  修改
    PtrS->rear = S;
}

int DeQueue(struct QNode *PtrS) {
    int Data;
    struct Node *S;
    if (PtrS->front->next == NULL) { //if (PtrS->front == NULL)   修改
        printf("队列空!\n");
        return 0;
    }
    S = PtrS->front->next; //S = PtrS->front; 修改
    if (PtrS->front->next == PtrS->rear) //只有一个元素  if (PtrS->front == PtrS->rear) 修改
        PtrS->front->next = PtrS->rear = NULL; //PtrS->front = PtrS->rear = NULL;   修改
    else
        PtrS->front->next = PtrS->front->next->next;  //PtrS->front = PtrS->front->next; 修改
    Data = S->Data;
    free(S);
    printf("%d出队\n", Data);
    return 0;
}

void outputQNode(struct QNode *PtrS) {
    struct Node *pMove = PtrS->front->next;
    while (pMove != NULL) {
        printf("%d\n", pMove->Data);
        pMove = pMove->next;
    }
}

int main() {
    struct QNode *PtrS;
    int i;
    PtrS = CreatQueue(); // CreatQueue();  修改
    for (i = 0; i < 10; i++) {
        EnQueue(PtrS, i);
    }
    for (i = 0; i < 10; i++) {
        DeQueue(PtrS);
    }
    return 0;
}