鱼C论坛

 找回密码
 立即注册
查看: 4866|回复: 5

这是小甲鱼老师的 删除链表的 问题

[复制链接]
发表于 2013-9-15 00:08:14 | 显示全部楼层 |阅读模式
5鱼币
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof (struct student )//student结构的大小

  5. struct student *creat();//自定义函数--创建链表
  6. struct student *del(struct student *head,int num);//自函数--del函数用于删除结点,*head即链表
  7.                                                   //的头指针,num是要删除的结点num

  8. void print(struct student *head);//自定义函数--打印链表

  9. struct student
  10. {
  11.         int num;
  12.         float score;
  13.         struct student *next;
  14. };

  15. int n;   //全局变量,用来记录存放了多少数据

  16. int main()
  17. {
  18.         struct student *stu,*p;
  19.         int n;

  20.         stu=creat(); //得到创建后的链表的头指针
  21.         p=stu;
  22.         print(p);

  23.         printf("Please enter the num to delete\n");
  24.         scanf("%d",&n);
  25.         print(del(p,n));  //调用函数del 进行删除

  26.         printf("\n\n");
  27.         system("pause");
  28. }
  29. //  创造链表
  30. struct student *creat()
  31. {
  32.         struct student *head;     //头指针
  33.         struct student *p1,*p2;
  34.        
  35.         p1=p2=(struct student *)malloc(LEN);
  36.        
  37.         printf("Please enter the num\n");
  38.         scanf("%d",&p1->num);
  39.         printf("Please enter the score\n");
  40.     scanf("%f",&p1->score);
  41.        
  42.         head=NULL;
  43.         n=0;
  44.         while(p1->num)
  45.         {
  46.                 n++;
  47.                 if(1==n)
  48.                 {
  49.                         head=p1;
  50.                 }
  51.         else
  52.                 {
  53.                         p2->next=p1;
  54.                 }
  55.             p2=p1;
  56.                 p1=(struct student *)malloc(LEN);
  57.                
  58.                 printf("Please enter the num:\n");
  59.                 scanf("%d",&p1->num);
  60.                 printf("Please enter the score:\n");
  61.                 scanf("%f",&p1->score);
  62.         }
  63.         p2->next = NULL;

  64.         return head;
  65. }
  66. void print(struct student *head)
  67. {
  68.         struct student *p;
  69.         printf("\nThere are %d records!\n",n);
  70.         p=head;
  71.         if(head)
  72.         {
  73.                 do
  74.                 {
  75.                         printf("学号是为%d的成绩是 %f \n",p->num,p->score);
  76.                         p=p->next;
  77.                 }while(p);
  78.         }
  79. }
  80. struct student *del(struct student *head,int num)
  81. {
  82.         struct student *p1,*p2;

  83.         if(NULL==head) // 如果头结点指向NULL,这是一个空指针,直接结束
  84.         {
  85.                 printf("\nThis list is null!\n");
  86.                 goto end;
  87.         }
  88.        
  89.         p1=head;
  90.         while(p1->num!=num && p1->next!=NULL)
  91.         {
  92.                 p2=p1;
  93.                 p1=p1->next;
  94.         }
  95.         if(num==p1->num) //将要删除的结点位于头结点的时候(特殊情况)
  96.         {
  97.                 if(p1==head)
  98.                 {
  99.                         head=p1->next;
  100.                 }
  101.                 else                //一般情况
  102.                 {
  103.                         p2->next=p1->next;
  104.                 }
  105.                 printf("\nDelete No:%d succeed!\n",num);
  106.                 n=n-1;  //全局变量,记录链表的数据个数
  107.         }
  108.         else
  109.         {
  110.                 printf("%d not been found!\n",num);

  111.         }
  112. end:
  113.      return head;
  114. }
复制代码


其中用到了malloc分配内存
可是并没有用 free 释放。。难道不释放无所谓吗?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-15 13:28:26 | 显示全部楼层
首先这个貌似是定义在函数里的函数运行完就会自动释放
再者反正就几个指针而已这还真的无所谓 撑死都不会到1kb 现在内存都是X GB的 无伤大雅
如果讲错请一笑而过
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-19 14:13:55 | 显示全部楼层
我看del的时候没有free,这个的确是问题。
在大的工程里面,可能会千百次调用这个链表函数,那么内存泄露就很多了。
我关于链表做了一个帖子你可以搜搜看,但是都是自己做的,仅仅供参考即可
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-25 10:30:53 | 显示全部楼层
新人,来看学习的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-26 13:27:21 | 显示全部楼层
用malloc函数申请的内存,最后应该要用free函数释放掉的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-9-26 21:16:28 | 显示全部楼层
真是难得给力的帖子啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-10 12:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表