yangyumizha 2017-09-27 18:19 采纳率: 100%
浏览 808
已采纳

链表删除操作失败,但是不知道具体原因,可能当局者迷吧,大神们帮忙看下,谢谢!!!

#include
#include
#include
struct stus
{
int date;
struct stus *next;
};

int output(struct stus *first)
{
struct stus *p;
p = first;
while(p)
{
printf("%d\n",p->date);
p = p->next;
}
return 0;//输出成功
}

int insert(struct stus *first,int n,int date)
{
struct stus *tmp,*stunew,*p;
stunew = (struct stus *)malloc(sizeof(struct stus));
tmp = first;
p = NULL;
n--;
while(tmp && n--)
{
p = tmp;
tmp = tmp->next;
}
if(tmp == NULL)
{
stunew->date = date;
p->next = stunew;
stunew->next = NULL;
return -1;
}
stunew->date = date;
stunew->next = p->next;
p->next = stunew;
return 0;//插入成功
}

int del(struct stus *first,int n)
{
struct stus *tmp = NULL,*p = NULL;
tmp = first;
while(tmp && n--)
{
printf("循环体\n");
p = tmp;
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("选择删除位置不对\n");
return -1;
}
if(tmp->next == NULL)
{
p->next = NULL;
free(tmp);
printf("选择删除尾节点\n");
return 1;//删除尾节点
}
printf("删除操作\n");
p->next = tmp->next;
free(tmp);
return 0;//删除成功
}

int find(struct stus *first,int date)
{
struct stus *tmp = first;
while(tmp)
{
if(tmp->date == date)
{
printf("%d\n--------看到你了----------\n",tmp->date);
return 0;
}
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("没有这个数\n-------------\n");
return NULL;
}
}

int main()
{
int i,j,k;
struct stus stu1,stu2,stu3,*p,*head;
stu1.date = 100;
stu2.date = 1000;
stu3.date = 10000;
head = &stu1;
stu1.next = &stu2;
stu2.next = &stu3;
stu3.next = NULL;

insert(&stu1,3,22);
find(&stu1,22);
printf("\n------------------\n");
output(&stu1);
printf("\n------------------\n");
del(&stu1,1);
output(&stu1);

}
输出结果:
图片说明

  • 写回答

2条回答 默认 最新

  • 小明学编程 2017-09-28 02:29
    关注

    因为你的节点数据,不是malloc申请的内容,而是main函数中的定义的临时变量,del函数中使用free去释放控件,就挂了。

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

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败