【问题标题】:Deleting a first node in the linked list or pop in the queue删除链表中的第一个节点或弹出队列
【发布时间】:2021-03-24 11:15:03
【问题描述】:

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

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

【问题讨论】:

    标签: c linked-list queue


    【解决方案1】:

    当您在 Head-&gt;next 推送您正在执行的元素时,但 pop 是从 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 指针,当您到达最后一个元素(即第一个插入的元素)时,使用@ 删除它987654329@指针。

    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) 的规则。

    在队列中,插入在后端完成,删除在前端完成,因此请相应地使用指针并实现。

    【讨论】:

    • 这是一个使用 LIFO 的常规堆栈,但我想要 FIFO 算法。
    • 那么您必须修改您的 pop 函数,因为它当前会删除最新的内容,要执行您想要的操作,您必须遍历到最后一个元素并删除
    • 你能帮我弄清楚吗@IrAM
    • 我无法弄清楚如何在该函数中制作确切的代码,因为没有任何效果
    • @JackJones,试着理解这个程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多