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 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决