zoukankan      html  css  js  c++  java
  • 单链表的最装逼写法

    include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    #define STAILQ_ENTRY(type)   
    struct {                     
      struct type *stqe_next;    
    }
    
    struct mbuf {
      uint32_t           magic;   /* mbuf magic (const) */
      STAILQ_ENTRY(mbuf) next;    /* next mbuf */
      uint8_t            *pos;    /* read marker */
      uint8_t            *last;   /* write marker */
      uint8_t            *start;  /* start of buffer (const) */
      uint8_t            *end;    /* end of buffer (const) */
    };
    
    #define STAILQ_HEAD(name, type)       
    struct name {                         
      struct type *stqh_first;            
      struct type **stqh_last;            
    }
    
    STAILQ_HEAD(mhdr, mbuf);
    
    #define STAILQ_NEXT(elm, field)    ((elm)->field.stqe_next)
    
    #define STAILQ_INSERT_TAIL(head, elm, field) do {  
      STAILQ_NEXT((elm), field) = NULL;                
      *(head)->stqh_last = (elm);                      
      (head)->stqh_last = &STAILQ_NEXT((elm), field);  
    } while (0)
    
    int main(void)
    {
      struct mhdr *mhdr = malloc(sizeof(struct mhdr));
      mhdr->stqh_first = NULL;
      mhdr->stqh_last  = &mhdr->stqh_first;
    
      int i;
      for (i = 0; i < 16; i++) {
        struct mbuf *mbuf = malloc(sizeof(struct mbuf));
        STAILQ_INSERT_TAIL(mhdr, mbuf, next);
        printf("%p ", mbuf);
      }
      printf("
    ");
    
      for (i = 0; i < 16; i++) {
        struct mbuf *mbuf = mhdr->stqh_first;
        printf("%p ", mbuf);
        mhdr->stqh_first = mhdr->stqh_first->next.stqe_next;
      }
      printf("
    ");
    
      return 0;
    }

    输出结果:

    [jabari@hbase-rs2-test ~]$ gcc a.c 
    [jabari@hbase-rs2-test ~]$ ./a.out 
    0x1804030 0x1804070 0x18040b0 0x18040f0 0x1804130 0x1804170 0x18041b0 0x18041f0 0x1804230 0x1804270 0x18042b0 0x18042f0 0x1804330 0x1804370 0x18043b0 0x18043f0 
    0x1804030 0x1804070 0x18040b0 0x18040f0 0x1804130 0x1804170 0x18041b0 0x18041f0 0x1804230 0x1804270 0x18042b0 0x18042f0 0x1804330 0x1804370 0x18043b0 0x18043f0
  • 相关阅读:
    SQLiteStudio免费的mac下的sqlite 客户端工具
    pip python mysqlclient 报各种错
    python Flask安装
    配置本地资源映射路径 addResourceHandlers
    微信支付 统一下单 签名错误
    utf8mb4_bin与utf8_genera_ci
    Java中Synchronized的用法(简单介绍)
    javaScript正则表达式的使用
    中文正则表达式匹配-正则中文匹配
    excel之CMMI-FP功能点估算辅助生成DET、RET、FTR、FP
  • 原文地址:https://www.cnblogs.com/superise/p/4185363.html
Copyright © 2011-2022 走看看