免费注册 查看新帖 |

Chinaunix

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

链表结点添加的小疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-18 11:39 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>

  4. typedef struct NumInfo
  5. {
  6.         int                                number;
  7.         struct NumInfo        *next;
  8. }NumInfo, *pNumInfo;

  9. void insertNum(pNumInfo *numNode, int num)
  10. {
  11.         pNumInfo node = (pNumInfo)malloc(sizeof(NumInfo));
  12.         memset(node, 0, sizeof(NumInfo));
  13.         node->number = num;
  14.         if (*numNode==NULL)
  15.                 *numNode = node;
  16.         else
  17.         {
  18.                 node->next = *numNode;
  19.                 *numNode = node;
  20.         }
  21. }

  22. int main()
  23. {
  24.         pNumInfo node;
  25.         insertNum(&node, 10);
  26.         return 0;
  27. }
复制代码
CPP里面有pNumInfo *&numNode,
然后在C中,我就习惯用**了。
这种方式写代码,有什么不好的地方呢、?

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
2 [报告]
发表于 2012-04-18 11:46 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2012-04-18 12:16 |只看该作者
回复 2# pmerofc

慧眼呵,我才难过呢。
  1.         if (*numNode!=NULL)
  2.                 node->next = *numNode;
  3.         *numNode = node;
复制代码
确实好看多了

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
4 [报告]
发表于 2012-04-18 12:20 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2012-04-18 16:09 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2012-04-19 14:09 |只看该作者
*numNode!=NULL 不用*吧,numNode 是个指针,直接numNode == NULL 去判断就可以了。
另外,for node allocation, should use safe guard, like :
pNumInfo node = (pNumInfo)malloc(sizeof(NumInfo));
Assert(node != NULL);
then,        memset(node, 0, sizeof(NumInfo));

in C++, it doesn't matter if you still use double star as reference, just like u said. **pointer.
However, *&pointer and **pointer indeed are not the same thing.

Chinese input method got something wrong, sorry.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP