zoukankan      html  css  js  c++  java
  • C语言之链表的使用

    一、协议中链表的应用

    最近在stm32F4调试网口,移植LwIP协议上去,里边的的结构体pbuf使用了链表,如下所示:

    struct pbuf {
      /** next pbuf in singly linked pbuf chain */
      struct pbuf *next;    	//指向下一个pbuf结构体,可以构成链表
    
      /** pointer to the actual data in the buffer */
      void *payload;        	//指向该pbuf真正的数据区
    
      /**
       * total length of this buffer and all next buffers in chain
       * belonging to the same packet.
       *
       * For non-queue packet chains this is the invariant:
       * p->tot_len == p->len + (p->next? p->next->tot_len: 0)
       */
      u16_t tot_len;         	//当前pbuf和链表中后面所有pbuf的数据长度,它们属于一个数据包
    
      /** length of this buffer */
      u16_t len;             	//当前pbuf的数据长度
    
      /** pbuf_type as u8_t instead of enum to save space */
      u8_t /*pbuf_type*/ type;  //当前pbuf的类型
    
      /** misc flags */
      u8_t flags;               //状态为,保留
    
      /**
       * the reference count always equals the number of pointers
       * that refer to this pbuf. This can be pointers from an application,
       * the stack itself, or pbuf->next pointers from a chain.
       */
      u16_t ref;				//该pbuf被引用的次数
    };
    

    二、链表的理解

    链表是一种常见的数据结构,它是动态地进行存储分配的一种结构,链表中的每一个结点的数据类型为结构体类型,结点有两部分成员组成:数据成员用于保存结点数据,地址成员则保存着指向下一个结构体类型结点的指针(即下一个结点的地址),链表的数据结构如下:

    struct node
    {
       int num;         //数据域
       struct node *p;  //指针域
    };
    

    所以对于上面的pbuf结构体中struct pbuf *next是指针域,指向下一个pbuf结构体,而其它定义就是数据域。

    三、链表的建立和输出

    根据下面的例子来看下链表是如何建立和输出:

    struct HAR
    {
       int x,y;
       struct HAR *p;
    }h[2];
    void main(void)
    {
       h[0].x=1;
       h[0].y=2;
       h[1].x=3;
       h[1].y=4;
       h[0].p=&h[1];
       h[0].p=h;
       printf("%d%d
    ",(h[0].p)->x,(h[1].p)->y);
    }
    
    

    结构数据h的结点关系如下所示,从中分析出(h[0].p)->x=h[1].x=3,(h[1].p)->y=h[0].y=2,所以输出的结果为32

    使用链表的好处是可以构造动态的数组,根据需要随时调整数组大小,以满足不同的需要,链表是在程序执行过程中按需要申请存储空间,不会出现资源浪费。

    四、参考文档

    http://blog.csdn.net/lpp0900320123/article/details/20356143

    by 羊羊得亿
    2017-11-01 ShenZhen

  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/yangxuli/p/7767562.html
Copyright © 2011-2022 走看看