开卷&&有益 2021-10-18 19:42 采纳率: 80%
浏览 27
已结题

问一下我写的链表清空模块为啥跑不起来?

//其他没毛病,就清空跑不来
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
    char name[10];
    struct student *next;
};
//打印 
void list(struct student *p)
{
    if(p->next ==NULL)
    {
        printf("没了没了");
    }
    else if(1)
    {
        while(1)
    {
        printf("%s\n", p->name);
        if(p->next != NULL)
        {
            p = p->next ;
        }
        else
        {
            break;
        }
    }
    }
    
}
//创造 
struct student *create()
{
    struct student *head, *current, *next;
    int n = 2;
    head = ((struct student *)malloc(sizeof(struct student)));
    strcpy(head->name   , "小李");
    current = head;
    while(n--)
    {
        next = ((struct student *)malloc(sizeof(struct student)));
        strcpy(next->name , "皮卡丘");
        
        current->next = next;
        current = next;
    
    }
    current->next  = NULL;
    return head;
     
}
//清空 
struct student *cc(struct student *p)
{
    struct student *current, *q;
    current = p;
    while(current->next !=NULL)
    {
        q = current->next ;
        delete(current);
        current = q;
    }
    current->next =NULL;
    printf("删除完了");  
    return p;
}
//主模块 
int main()
{
    struct student *p;
    p = create();
    list(p);
    cc(p);
    list(p);
     
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 关注

    你删除最后要更新p,而不是更新current
    改为 p->next = NULL;

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月18日

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题