tnblog
首页
视频
资源
登录

c 单链表示例

5920人阅读 2019/8/21 22:28 总访问:3467286 评论:1 收藏:0 手机
分类: 传说中的c

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "link_list.h"
  5. #define my_malloc malloc
  6. #define my_free free
  7. //定义一个形状
  8. struct rect
  9. {
  10. int x;
  11. int y;
  12. int m;
  13. int h;
  14. };
  15. struct circle
  16. {
  17. int x;
  18. int y;
  19. };
  20. struct shape
  21. {
  22. int type;
  23. union
  24. {
  25. struct rect r;
  26. struct circle c;
  27. }u;
  28. struct link_node link;
  29. };
  30. //创建一个空间
  31. static struct shape* alloc_shape()
  32. {
  33. struct shapeone = my_malloc(sizeof(struct shape));
  34. memset(one, 0sizeof(struct shape));
  35. return one;
  36. }
  37. //释放
  38. static void free_shape(struct shape* a)
  39. {
  40. my_free(a);
  41. }
  42. //清理单链表中所有内存
  43. static void All_shape(hhlinknode all)
  44. {
  45. if (!all)
  46. {
  47. return;
  48. }
  49. struct shapeelem = LINK_TO_ELEM(allstruct shapelink);
  50. if (all->next)
  51. {
  52. All_shape(all->next);
  53. }
  54. free_shape(elem);
  55. }
  56. int main()
  57. {
  58. hhlinknode Thead = NULL;
  59. struct shapeshapeone = alloc_shape();
  60. shapeone->type = 1;
  61. list_insert_head(&Thead, &shapeone->link);
  62. shapeone = alloc_shape();
  63. shapeone->type = 2;
  64. list_insert_head(&Thead, &shapeone->link);
  65. shapeone = alloc_shape();
  66. shapeone->type = 3;
  67. list_insert_tail(&Thead, &shapeone->link);
  68. hhlinknode walk = Thead;
  69. while (walk)
  70. {
  71. //通过偏移指针获取到类型
  72. struct shapeelem = LINK_TO_ELEM(walkstruct shapelink);
  73. //struct shape* elem = ((struct shape*)((unsigned char*)walk - (unsigned char*)(&((struct shape*)NULL)->link)));
  74. printf("%d \n", elem->type);
  75. walk = walk->next;
  76. }
  77. list_insert_remove(&Thead, &shapeone->link);
  78. All_shape(Thead);
  79. system("pause");
  80. return 0;
  81. }


单链表:

link_list.h

  1. #ifndef _LINK_LIST_H_
  2. #define _LINK_LIST_H_
  3. typedef struct link_node{
  4. struct link_nodenext;
  5. }hlinknode,*hhlinknode;
  6. typedef struct link_node *list_head;
  7. //单链表头部添加
  8. void list_insert_head(hhlinknode* header, hhlinknode node);
  9. //单链表尾部添加
  10. void list_insert_tail(hhlinknode* header, hhlinknode node);
  11. //单链表指定节点删除
  12. void list_insert_remove(hhlinknode* header, hhlinknode node);
  13. //#define LINK_TO_ELEM(link,elem_type,mem_name) (((elem_type)*)(((unsigned char*)link)-(unsigned char*)(&((elem_type*)NULL)->mem_name)))
  14. //通过指针
  15. #define LINK_TO_ELEM(link,elem_type,mem_name) \
  16. ((elem_type*)((unsigned char*)link - (unsigned char*)(&((elem_type*)NULL)->mem_name)));
  17. #endif


link_list.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "link_list.h"
  5. void list_insert_head(hhlinknode* header, hhlinknode node)
  6. {
  7. hhlinknode* walk = header;
  8. node->next = *walk;
  9. *walk = node;
  10. }
  11. void list_insert_tail(hhlinknode* header, hhlinknode node) 
  12. {
  13. hhlinknode* walk = header;
  14. while (*walk)
  15. {
  16. walk = &((*walk)->next);
  17. }
  18. node->next = NULL;
  19. *walk = node;
  20. }
  21. void list_insert_remove(hhlinknode* header, hhlinknode node) {
  22. hhlinknode* walk = header;
  23. while (*walk)
  24. {
  25. if (*walk==node)
  26. {
  27. *walk = node->next;
  28. node = NULL;
  29. return;
  30. }
  31. //这里必须要改指针的指针,不是改指针的值
  32. walk = &(*walk)->next;
  33. }
  34. }



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

评价

剑轩

2019/8/22 10:15:02

c语言很厉害,各种东西都可以写

css弹性盒子,flex布局

css弹性盒子由于版本不同浏览器问题造成了一些不同的写法display:flexbox;在google浏览器中如果使用下面的写法就不行displa...

css图片和文字对齐问题

文字和图片写到一排经常会出现对不齐的问题 这样感觉图片会上来一点没有和文字对齐,如下图 但是如果修改下html结...

GitHub 上传项目

补充简化方法:登录git创建项目--&gt;拉取刚刚创建的项目--&gt;复制需要的代码进去--&gt;上传提交即可先拉取项目在上传代码...

NET core 使用 EF code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

Windows平台分布式架构实践 - 负载均衡

原文地址: https://www.cnblogs.com/atree/p/windows_loadbalancer.html 概述  最近.NET的世界开始闹腾了,微软官方终...

css实现简单矩形对话框

在前端做项目时,我们可能会遇到写对话框的需求,这次做视频会议页面就遇到了,记录下日后有个参照。//网页部分 &lt;divcla...

cAPS.NET 保存base64位格式的图片

publicvoidUpload() { //取出图片对应的base64位字符 stringimgBase=Request[&quot;imgBase&quot;]; //c#里边的base6...

使用OLEDB读取不同版本Excel的连接字符串设置

使用OleBD读取excel的时候,excel不同的版本,连接字符串的写法也会不一样。///&lt;summary&gt; ///读取excel ///&lt;/su...

vs2017 对 cOM 组件的调用返回了错误 HRESULT E_FAIL

vs2017添加引用报错 对 COM 组件的调用返回了错误 HRESULT E_FAIL 1.以管理员身份打开vs2017开发人员命令指示符 2...

分布式服务架构与微服务架构概念的区别与联系

分布式:分散压力。微服务:分散能力。当下理解分布式:不同模块部署在不同服务器上作用:分布式解决网站高并发带来问题集...

分布式-微服务-集群的区别

1.分布式将一个大的系统划分为多个业务模块,业务模块分别部署到不同的机器上,各个业务模块之间通过接口进行数据交互。区...

NPOI操作excel 2007/2010版本

HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xlsXSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx先...

这样学英语三个月超过你过去学三年

本文作者三年间从四级勉强及格到高级口译笔试210,口试232。找工作面试时给其口试的老外考官听了一分钟就说你的英语不用考...

EasyUI弹窗批量修改combogrid下拉框的值

JS方法//点击弹出批量修改框 UpdateLot:function(){ varrow=$(&quot;#dg&quot;).datagrid(&quot;getChecked&quot;); if(...

js与controller中分割字符串的方法

js: varstr=OpenRule; varstrs=newArray(); strs=str.split(&quot;,&quot;); for(vari=0;i&lt;strs.length;i++){ $(&q...

如何修改cSS中存在的element.style内联样式

改腾讯地图的时候调整了下样式,发现样式一直存在问题,修改style里面的值,一点用都没有,html中这个值还找不到是在哪里出...
这一世以无限游戏为使命!
排名
2
文章
634
粉丝
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
欢迎加群交流技术