zoukankan      html  css  js  c++  java
  • 第一次理解通用链表

     1 #ifndef _LIST_H
      2 #define _LIST_H
      3 #include"stdio.h"
      4 #define _INLINE_ static inline
      5
      6 struct list_head {
      7                 struct list_head *next, *prev;
      8 };
      9
     10 #define LIST_HEAD_INIT(name) {&(name), &(name)}
     11
     12 #define LIST_HEAD(name)
     13                 struct list_head name = LIST_HEAD_INIT(name)
     14
     15 #define INIT_LIST_HEAD(ptr) do {
     16                 (ptr)->next = (ptr); (ptr)->prev = (ptr);
     17 } while (0)
     18
     19 _INLINE_ void __list_add(struct list_head *add,
     20                                 struct list_head *prev,
     21                                                 struct list_head *next)
     22 {
     23                 next->prev = add;
     24                         add->next = next;
     25                                 add->prev = prev;
     26                                         prev->next = add;
     27 }
     28
     29 _INLINE_ void list_add(struct list_head *add, struct list_head *head) {
     30                 __list_add(add, head, head->next);
     31 }
     32
     33 _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)
     34 {
     35                 __list_add(add, head->prev, head);
     36 }
     37
     38 _INLINE_ void __list_del(struct list_head *prev, struct list_head *next)
     39 {
     40                 next->prev = prev;
     41                         prev->next = next;
     42 }
     43
     44 _INLINE_ void list_del(struct list_head *entry)
     45 {
     46                 __list_del(entry->prev, entry->next);
     47 }
     48
     49 _INLINE_ void list_del_init(struct list_head *entry)
     50 {
     51                 __list_del(entry->prev, entry->next);
     52                         INIT_LIST_HEAD(entry);
     53 }
     54
     55 _INLINE_ int list_empty(struct list_head *head)
     56 {
     57                 return head->next == head;
     58 }
     59 
      76 #define list_entry(ptr, type, member)
     77                 ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
     78
     79 #define list_for_each(pos, head)
     80                 for (pos = (head)->next; pos != (head); pos = pos->next)
     81
     82 #define list_for_each_safe(pos, pnext, head)
     83                 for (pos = (head)->next, pnext = pos->next; pos != (head);
     84                                                         pos = pnext, pnext = pos->next)
     85
     86 #undef _INLINE_
     87 #endif
     88
    ~          1 #include"list.h"
      2 typedef struct m
      3 {
      4         int a;
      5         int b;
      6         struct list_head list;
      7 }Q;
      8 int main(int argc,char *argv[])
      9 {
     10         int i;
     11         Q *p=NULL;
     12         struct list_head *ptr=NULL;
     13         LIST_HEAD(t);
     14         for(i=0;i<10;i++)
     15         {
     16                 p=(Q*)malloc(sizeof(Q));
     17                 p->a=i;
     18                 p->b=i+2;
     19                 list_add(&(p->list),&t);
     20         }
     21         list_for_each(ptr,&t)
     22         {
     23                 p=list_entry(ptr,Q,list);
     24                 printf("%d %d ",p->a,p->b);
     25
     26         }
     27         return 0;
     28 }            

  • 相关阅读:
    Tip#66:你知道吗?如何在输入属性值时自动插入双引号
    使用 Apache MINA 开发高性能网络应用程序(转载)
    Faceted Search with Solr
    solr dataimport 数据导入源码分析 补充
    Apache Tika
    MiddlegenHibernate的配制和使用(jtds连接sqlserver数据库)
    汉语转拼音之pinyin4j(转载)
    使用Tika进行非结构化内容的读写1
    使用Java NIO编写高性能的服务器
    solr dataimport 数据导入源码分析(十)总结
  • 原文地址:https://www.cnblogs.com/3ddan/p/3278811.html
Copyright © 2011-2022 走看看