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 }            

  • 相关阅读:
    70.BOM
    69.捕获错误try catch
    68.键盘事件
    523. Continuous Subarray Sum
    901. Online Stock Span
    547. Friend Circles
    162. Find Peak Element
    1008. Construct Binary Search Tree from Preorder Traversal
    889. Construct Binary Tree from Preorder and Postorder Traversal
    106. Construct Binary Tree from Inorder and Postorder Traversal
  • 原文地址:https://www.cnblogs.com/3ddan/p/3278811.html
Copyright © 2011-2022 走看看