免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2812 | 回复: 6
打印 上一主题 下一主题

[C] 请教各位大神一个关于动态链表的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-06 14:28 |只看该作者 |倒序浏览
新手勿喷,今天学习到动态链表相关知识,根据文档学习后自己编了个小程序,但是为什么p2-next = NULL,得不到正确的结果,反而是p2 = NULL能得到正确结果,详见红字部分代码。

  1. /***********************************
  2. **程序:test_node_1(线性链表测试)
  3. **作者:lwp
  4. **日期:20140506
  5. **版本:v1.0
  6. **功能:创建一个存放3组数据的动态链表
  7. **
  8. ***********************************/

  9. #include <stdio.h>
  10. #include <string.h>

  11. #define LEN sizeof(struct student)

  12. typedef struct student{
  13.         char name[10];
  14.         int age;
  15.         struct student *next;
  16. };

  17. struct student *creat();

  18. int main(int argc,char* argv[])
  19. {
  20.         struct student *p;

  21.         p = creat();

  22.         do
  23.         {
  24.                 printf("name->%s,age->%d\n",p->name,p->age);

  25.                 p = p->next;

  26.         }while(p != NULL);

  27.         return 0;
  28. }

  29. struct student *creat(void)
  30. {
  31.         struct student *head,*p1,*p2;

  32.         p1=p2=(struct student*)malloc(LEN);

  33.         scanf("%s%d",&p1->name,&p1->age);

  34.         head = NULL;//线性链表头指向一个空结点

  35.         head = p1; //头指针head指向p1分配的首结点

  36.         while(1)
  37.         {
  38.                 p1 = (struct student*)malloc(LEN); //p1继续开辟新结点

  39.                 scanf("%s%d",&p1->name,&p1->age);

  40.                 if(p1->age == 0) break;

  41.                 p2->next = p1; //p1开辟的新节点链接到p2结点后面

  42.                 p2 = p1;
  43.         }

  44.         printf("p2->name=%s,p2->age=%d\n",p2->name,p2->age);

  45.         [color=Red]p2->next = NULL;[/color]

  46.         free(p1);
  47.         free(p2);

  48.         return(head); //返回链表头指针
  49. }
复制代码

论坛徽章:
2
申猴
日期:2014-04-17 14:37:17CU十四周年纪念徽章
日期:2018-06-23 16:03:03
2 [报告]
发表于 2014-05-06 14:52 |只看该作者
free(p2); 被你释放了。

论坛徽章:
11
巨蟹座
日期:2013-12-23 11:12:14双子座
日期:2014-08-28 09:14:55子鼠
日期:2014-07-25 16:21:22摩羯座
日期:2014-07-23 15:17:47摩羯座
日期:2014-05-30 13:09:05午马
日期:2014-04-30 18:10:00天秤座
日期:2014-04-25 12:12:00申猴
日期:2014-04-22 11:30:15午马
日期:2014-03-07 16:06:40辰龙
日期:2013-12-25 18:36:00摩羯座
日期:2014-09-02 17:00:55
3 [报告]
发表于 2014-05-06 14:59 |只看该作者
这注释感觉蛮好~~

论坛徽章:
0
4 [报告]
发表于 2014-05-06 15:01 |只看该作者
回复 2# tklist
正解,还有个疑问,我的p2->next = NULL是在free之前,为什么还是会导致这种情况发生呢


   

论坛徽章:
2
申猴
日期:2014-04-17 14:37:17CU十四周年纪念徽章
日期:2018-06-23 16:03:03
5 [报告]
发表于 2014-05-06 17:29 |只看该作者
p2是链表里面最后一个元素。p2->next=null 这个和free(p2)没有任何关系。回复 4# lwphappy


   

论坛徽章:
1
双子座
日期:2014-05-02 22:43:41
6 [报告]
发表于 2014-05-06 23:15 |只看该作者
The important key point is that , if the pionter is NULL, THE free() does't do anything.
So the value in last list inode does't change.

论坛徽章:
1
双子座
日期:2014-05-02 22:43:41
7 [报告]
发表于 2014-05-06 23:15 |只看该作者
The important key point is that , if the pionter is NULL, THE free() does't do anything.
So the value in last list inode does't change.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP