重名大师 2023-02-15 22:29 采纳率: 96.6%
浏览 14
已结题

c语言链表的删除节点这个代码有什么问题吗

利用c语言里的链表删除大于k的节点,以0为结尾,输入k和链表,输出删除后的链表。(只会c语言,还没有学习c++)
不知道为什么代码运行错误,我是根据书上的链表删除的公式写的删除函数的,但是完全没发删除,感觉很苦恼
例子:
输入:2000
2001 2002 1999 1998 2021 1997 0
输出:1999 1998 1997


```c
#include<stdio.h>
#include<malloc.h>
struct cell
{
    int year;
    struct cell*next;
};
struct cell*build(void)
{
    struct cell*head,*p,*tmp;
    int n;
    head=p=tmp=NULL;
    scanf("%d",&n);
    if(n==0)
    {
        return head;
    }
    p=(struct cell*)malloc(sizeof(struct cell));
    p->year=n;
    p->next =NULL;
    head=p;
    scanf("%d",&n);
    while(n!=0)
    {
        tmp=(struct cell*)malloc(sizeof(struct cell));
        tmp->year=n;
        tmp->next=NULL;
        p->next=tmp;
        p=p->next;
        scanf("%d",&n);
    }
    return head;
};
struct cell*delect(struct cell*head,int k)
{
    struct cell*p,*p0,*p1;
    p=head;
    p0=NULL;
    if(head==NULL){return head;}
    while(p->year>k)
    {p=p->next;free(p0->next);p0->next=p;

    }

   head=p;

    while(p!=NULL)
    {
        if(p->year>k){p=p->next;free(p0->next);p0->next=p;}
        p0=p;p=p->next;

    }
    return head;
};
void print(struct cell*head)
{
    struct cell*p;
    p=head;
    if(head==NULL){printf("NULL");return;}
    while(p!=NULL)
    {
        printf("%d",p->year);
        if(p->next!=NULL){printf(" ");}
    }
}
void release(struct cell*head)
{
    struct cell*p0,*p;
    p=head;
    while(p!=NULL)
    {
        p0=p;p=p->next;free(p0);
    }
}
int main()
{
    struct cell*head;int k;
    scanf("%d",&k);
    head=build();

    head=delect(head,k);
    print(head);
    release(head);
    return 0;
}



  • 写回答

3条回答 默认 最新

  • 量化研究所 2023-02-15 23:23
    关注

    您的代码中存在一些问题:

    在 delect() 函数中,当链表头的值大于 k 时,您直接让 head 指向当前节点 p,但是却没有释放原来的头节点空间,导致内存泄漏。可以先将当前节点 p 存到 p1 中,再释放 p0。
    
    在 print() 函数中,每输出一个节点的值后都应该将 p 指向下一个节点,否则会造成死循环输出。
    
    在输出节点的值时,应该加上换行符,否则多个值会输出在同一行,不易阅读。
    

    下面是修改后的代码,您可以参考一下:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct cell {
        int year;
        struct cell *next;
    };
    
    struct cell *build(void) {
        struct cell *head, *p, *tmp;
        int n;
        head = p = tmp = NULL;
        scanf("%d", &n);
        if (n == 0) {
            return head;
        }
        p = (struct cell*)malloc(sizeof(struct cell));
        p->year = n;
        p->next = NULL;
        head = p;
        scanf("%d", &n);
        while (n != 0) {
            tmp = (struct cell*)malloc(sizeof(struct cell));
            tmp->year = n;
            tmp->next = NULL;
            p->next = tmp;
            p = p->next;
            scanf("%d", &n);
        }
        return head;
    }
    
    struct cell *delect(struct cell *head, int k) {
        struct cell *p, *p0, *p1;
        p = head;
        p0 = NULL;
        if (head == NULL) {
            return head;
        }
        while (p->year > k) {
            p1 = p;
            p = p->next;
            free(p1);
        }
    
        head = p;
    
        while (p != NULL) {
            if (p->year > k) {
                p1 = p;
                p = p->next;
                free(p1);
                p0->next = p;
            } else {
                p0 = p;
                p = p->next;
            }
        }
        return head;
    }
    
    void print(struct cell *head) {
        struct cell *p;
        p = head;
        if (head == NULL) {
            printf("NULL\n");
            return;
        }
        while (p != NULL) {
            printf("%d\n", p->year);
            p = p->next;
        }
    }
    
    void release(struct cell *head) {
        struct cell *p0, *p;
        p = head;
        while (p != NULL) {
            p0 = p;
            p = p->next;
            free(p0);
        }
    }
    
    int main() {
        struct cell *head;
        int k;
        scanf("%d", &k);
        head = build();
    
        head = delect(head, k);
        print(head);
        release(head);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 已采纳回答 3月16日
  • 创建了问题 2月15日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法