风&雨 2022-02-16 20:43 采纳率: 100%
浏览 30
已结题

链表删除问题,代码有问题,求解

下图是问题:

img


解答如图问题时,发现删除操作有问题,不出结果,直到其中两个条件语句(已在代码中标出)交换位置后,可以得到正确结果,不知道为什么,特来请教。
以下是图中问题解决代码。


//这个是有问题的代码
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
    int data;
    struct node *next;
} Node, *LinkList;
LinkList Listinit() {
    Node *L;
    L = (Node*)malloc(sizeof(Node));
    if (L == NULL)exit(0);
    L->next = NULL;
    return L;
}
LinkList creat(int n) {
    Node *L;
    int x;
    L = (Node*)malloc(sizeof(Node));
    L->next = NULL;
    Node *r;
    r = L;
    while (n--) {
        scanf("%d", &x);
        Node *p;
        p = (Node*)malloc(sizeof(Node));
        p->data = x;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    return L;
}
LinkList delet(Node *L, int x) {
    Node *p, *pre;
    p = L->next;
    while (p->next) {
        //以下代码与另一个有所不同
        if (p->data == x) {
            pre->next = p->next;
            free(p);
        }
        if (p->data != x) {
            while (p->data != x && p->next) {
                pre = p;
                p = p->next;
            }
        }
        //以上代码与另一个有所不同
        p = pre->next;
    }
    return L;
}
void print(Node *L) {
    Node *p = L->next;
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
}
int main() {
    Node *L;
    int x, n;
    scanf("%d", &n);
    L = Listinit();
    L = creat(n);
    scanf("%d", &x);
    L = delet(L, x);
    print(L);
    return 0;
}


//这个是没有问题的代码
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
    int data;
    struct node *next;
} Node, *LinkList;
LinkList Listinit() {
    Node *L;
    L = (Node*)malloc(sizeof(Node));
    if (L == NULL)exit(0);
    L->next = NULL;
    return L;
}
LinkList creat(int n) {
    Node *L;
    int x;
    L = (Node*)malloc(sizeof(Node));
    L->next = NULL;
    Node *r;
    r = L;
    while (n--) {
        scanf("%d", &x);
        Node *p;
        p = (Node*)malloc(sizeof(Node));
        p->data = x;
        r->next = p;
        r = p;
    }
    r->next = NULL;
    return L;
}
LinkList delet(Node *L, int x) {
    Node *p, *pre;
    p = L->next;
    while (p->next) {
        //以下代码与另一个有所不同 
        if (p->data != x) {
            while (p->data != x && p->next) {
                pre = p;
                p = p->next;
            }
        }
        if (p->data == x) {
            pre->next = p->next;
            free(p);
        }
        //以上代码与另一个有所不同 
        p = pre->next; 
    }
    return L;
}
void print(Node *L) {
    Node *p = L->next;
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
}
int main() {
    Node *L;
    int x, n;
    scanf("%d", &n);
    L = Listinit();
    L = creat(n);
    scanf("%d", &x);
    L = delet(L, x);
    print(L);
    return 0;
}

想了很久没找到答案,很想知道为什么,希望知道原因的朋友不吝评论,感谢。

  • 写回答

3条回答 默认 最新

  • 谛凌 2022-02-16 21:06
    关注

    因为交换顺序之前,你第一个if里面可能会释放p的内存,所以第二个if就没法使用p,解决办法:

    • 1.像你现在这样,交换顺序
    • 2.将原来的第二个if改成else if,改了以后,如果执行了if,就不会执行else if了

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月17日
  • 已采纳回答 2月17日
  • 创建了问题 2月16日

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表