删除链表中的第一个节点或弹出队列

Deleting a first node in the linked list or pop in the queue

我的程序断点了,我觉得算法不错。我想删除链表或队列中的第一个节点,等等。关键是我通常可以将元素添加到堆栈,但不能删除它们,因为它会触发断点。为什么?我想在不使用前后指针的情况下删除第一个元素。那可能吗? 我的代码:

#define _CRT_SECURE_NO_WARNINGS
    #include<stdlib.h>
    #include<stdio.h>
    #include<time.h>

    #define MAX 100
    #define MIN 10

    typedef struct node* Queue;

    struct node {
        int el;
        Queue next;
    };

    void push(int x, Queue S);
    void pop(Queue S);
    void show(Queue S);


    int main() {
        int sl;
        char c;

        struct node Head;
        Head.next = NULL;

        srand(time(0));

        while (1) {
            printf("Queue\n\t->D to push\n\t->I to pop\n\t\n\t->E for exit\n\t->Choice: ");
            scanf("%c", &c);
            switch (c) {
            case'd':
            case'D':
                sl = MIN + rand() % ((MAX - MIN) + 1);
                push(sl, &Head);
                show(Head.next);
                break;
            case'i':
            case'I':
                pop(&Head);
                printf("After pop:\n");
                show(Head.next);
                break;
            case'e':
            case'E':
                return 0;
                break;
            default:
                printf("A mistake has occured.\n");
            }
        }
        return 0;
    }
      void push(int x, Queue S) {
        Queue q;
        q = (Queue)malloc(sizeof(struct node));

        q->el = x;
        q->next = S->next;
        S->next = q;
    }

    void pop(Queue S) {
    Queue temp,pre;
    temp = S->next;
    pre=NULL;
    while(temp && temp->next) {
        pre = temp;
        temp = temp->next;
    }
    free(pre->next);
    pre->next = NULL;
}
    void show(Queue S) {
        while (S != NULL)
        {
            printf("%d", S->el);
            S = S->next;
            printf("\n");
        }
    }

当你在 Head->next 推送你正在做的元素时,但是 pop ing 是从 Head 完成的,那是一个无效的地址,你会看到问题。

注意:我还没有处理指针 NULL 检查,最好在您尝试 next 任何指针

时进行这些检查
void pop(Queue S) {
    Queue temp;
    temp = S->next;
    //S = S->next;
    S->next = temp->next;
    free(temp);
    temp = NULL;
}

要删除第一个插入的元素,您必须遍历此列表并记下变量 pre 此处使用的 previous 指针,以及当您到达最后一个元素(即插入的元素)时首先)使用 pre 指针删除它。

void pop(Queue S) {
    Queue temp,pre = NULL;
    temp = S->next;
    while(temp && temp->next) {
        pre = temp;
        temp = temp->next;
    }
    if(pre) {
        free(pre->next);
        pre->next = NULL;
    }
}

但是,我强烈建议您遵循 Stack(LIFO) 和 Queue(FIFO) 的规则。

在队列中,插入在后端,删除在前端,所以使用相应的指针并实现。