0
Follow
0
View

How to solve the problem of chain structure # of ##i queue?

cys23_2012 注册会员
2023-02-28 01:43

This answer references ChatGPT
In your code, there are several problems:

1. There is no memory allocated for the pointer PtrS in main, so when EnQueue and DeQueue are called, the program accesses the unallocated memory, causing an unknown error.

2. In the EnQueue function, the next member of the pointer S should point to NULL instead of PtrS-> rear, because PTRL rear is the pointer to the tail element, and the next member variable of the tail element should be NULL, representing the tail.

3. In the EnQueue function, we should first put PtrS-> rear's next member variable points to S, and then PtrS-> rear points to S to add the new element S to the rear of the line. This problem may be caused by error 2 above.

4. In the DeQueue function, we need to judge PtrS-> Whether front is NULL instead of PtrS-> rear.

5. In the DeQueue function, we should first point the next member of S to PtrS-> front-> Eg. next, put it on. front points to PtrS. front-> next, the memory space occupied by S is finally released.

6. In the outputQNode function, you should start from PtrS-> front-> next started walking through the linked list because front is just a head node, not the actual head element.

#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);
    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;
}


cxmingko 注册会员
2023-02-28 01:43
cuiyanlong82 注册会员
2023-02-28 01:43

Please see the comments for the changes:

#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);
    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;
}

ecec_011 注册会员
2023-02-28 01:43

Could you send me the question?