tnblog
首页
视频
资源
登录

5769人阅读 2019/8/23 9:47 总访问:3658493 评论:0 收藏:0 手机
分类: 传说中的c

【Note】:1,在声明结构体的时候,自己取的别名自己是不能用的

                2,可以通过偏移指针位数来获取数据

封装树头文件

Demo26.h

  1. #ifndef _LINK_TREE_H_
  2. #define _LINK_TREE_H_
  3. #define LINK_TO_ELEM(link,elem_type,mem_name) \
  4. ((elem_type*)((unsigned char*)link - (unsigned char*)(&((elem_type*)NULL)->mem_name)));
  5. typedef struct tree_link
  6. {
  7. struct tree_linkparent; //指向它的父节点
  8. struct tree_linkchildren;//指向它的子节点
  9. struct tree_linkbrother;//指向它的兄弟节点
  10. }treelink,*ztreelink;
  11. typedef struct Tree_Master
  12. {
  13. int num;
  14. char name[16];
  15. treelink link;
  16. }TreeMaster;
  17. TreeMaster* alloc_item_node(const char* name);
  18. //添加节点
  19. void link_parent(ztreelink parent, ztreelink children);
  20. //删除节点
  21. void link_remove(ztreelink node);
  22. //遍历节点
  23. void ideration(ztreelink parent, char* oneparentsname);
  24. #endif

封装树的c文件

Demo26.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Demo26.h"
  5. #define my_free free
  6. TreeMaster* alloc_item_node(const char* name) {
  7. TreeMaster* tree = malloc(sizeof(TreeMaster));
  8. memset(tree, 0sizeof(TreeMaster));
  9. strcpy(tree->name, name);
  10. (&(tree)->link)->brother = NULL;
  11. (&(tree)->link)->children = NULL;
  12. (&(tree)->link)->parent = NULL;
  13. return tree;
  14. }
  15. void link_parent(ztreelink parent, ztreelink children)
  16. {
  17. //给子节点付parent
  18. children->parent = parent;
  19. if (parent == NULL)
  20. {
  21. return;
  22. }
  23. //用一个walk接收parent下子节点的地址
  24. ztreelink* walk = &((parent)->children);
  25. //循环找到最后一个
  26. while (*walk)
  27. {
  28. walk = &(*walk)->brother;
  29. }
  30. //添加该子节点
  31. *walk = children;
  32. }
  33. void ideration(ztreelink lord,char* oneparentsname)
  34. {
  35. if (lord == NULL)
  36. {
  37. return;
  38. }
  39. TreeMaster* one = LINK_TO_ELEM(lord, TreeMaster, link);
  40. if (oneparentsname != ""&& oneparentsname != NULL)
  41. {
  42. printf("parents: %s;children: %s; \n", oneparentsname, one->name);
  43. }
  44. else
  45. {
  46. printf("king: %s \n", one->name);
  47. }
  48. ztreelink* all = &((lord)->children);
  49. while (*all)
  50. {
  51. //获取父节点数据
  52. one = LINK_TO_ELEM(&(*(*all)->parent), TreeMaster, link);
  53. //获取父节点name数据
  54. char* parentsname = one->name;
  55. //获取子节点数据
  56. one = LINK_TO_ELEM(*all, TreeMaster, link);
  57. //获取子节点数据
  58. printf("parents: %s;children: %s; \n", parentsname, one->name);
  59. if ((*all)->children!=NULL)
  60. {
  61. ideration((*all)->children, one->name);
  62. }
  63. all = &(*all)->brother;
  64. }
  65. }
  66. void link_remove(ztreelink node) 
  67. {
  68. ztreelink* walk = node->parent->children;
  69. ztreelink* tbother = node->brother;
  70. TreeMaster* one;
  71. while (*walk)
  72. {
  73. if (*walk==node)
  74. {
  75. if (*tbother)
  76. {
  77. *walk = *tbother;
  78. }
  79. else
  80. {
  81. *walk = NULL;
  82. }
  83. break;
  84. }
  85. *walk = (*walk)->brother;
  86. }
  87. TreeMaster* one = LINK_TO_ELEM(node, TreeMaster, link);
  88. //将数据模块直接性清理掉
  89. my_free(one);
  90. }

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Demo26.h"
  5. int main()
  6. {
  7. ztreelink node1 = NULL;
  8. TreeMaster* root = alloc_item_node("A");
  9. node1 = &root->link;
  10. TreeMaster* root1 = alloc_item_node("B");
  11. TreeMaster* root2 = alloc_item_node("C");
  12. TreeMaster* root3 = alloc_item_node("D");
  13. link_parent(node1, &root1->link);
  14. link_parent(node1, &root2->link);
  15. link_parent(&root2->link, &root3->link);
  16. ideration(&root2->link,"");
  17. system("pause");
  18. return 0;
  19. }


请诸位自行测试。


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价
这一世以无限游戏为使命!
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术