1. 程式人生 > >資料結構之核心連結串列

資料結構之核心連結串列

核心連結串列設計的非常巧妙,但也不是什麼難理解的內容,關於核心連結串列的介紹網上有很多,這裡就不贅述了,來個使用的例子吧。

list.h

#ifndef HS_KERNEL_LIST_H
#define HS_KERNEL_LIST_H

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) \
      (type *)( (char *)ptr - offsetof(type,member) )

struct list_head {
      struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

static inline void __list_add(struct list_head *new,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

static inline void __list_del_entry(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

static inline void list_del_init(struct list_head *entry)
{
	__list_del_entry(entry);
	INIT_LIST_HEAD(entry);
}

#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

#define __list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)


#endif

main.c

#include <stdio.h>
#include <stdlib.h>

#include "list.h"

#define NAMESIZE 32

struct score {
      int id;
      char name[NAMESIZE];
      int math;
      struct list_head node;
};

void print_s(void *data)
{
      struct score *d = data;
      printf("%d %s %d\n", d->id, d->name, d->math);
}

int main(void)
{
      struct score *datap;
      int i;
      struct list_head *cur;

      LIST_HEAD(list);

      for (i = 0; i < 7; i++) {
	    datap = malloc(sizeof(*datap));
	    /* if error */

	    datap->id = i;
	    datap->math = 100 - i;
	    snprintf(datap->name, NAMESIZE, "stu%d", i);

	    list_add(&datap->node, &list);
      }

      __list_for_each(cur, &list) {
	    datap = list_entry(cur, struct score, node);
	    print_s(datap);
      }

#if 0
      __list_for_each(cur, &list) {
	    datap = list_entry(cur, struct score, node);
	    if (datap->id == 9) {
		  __list_del_entry(&datap->node);
		  free(datap);
	    }
      }
      printf("\n");
      __list_for_each(cur, &list) {
	    datap = list_entry(cur, struct score, node);
	    print_s(datap);
      }
#endif

      __list_for_each(cur, &list) {
	    datap = list_entry(cur, struct score, node);
	    if (datap->id == 5) {
		  break;
	    }
      }
      printf("\n");
      if (cur == &list) {
	    printf("Can not find.\n");
      } else {
	    print_s(datap);
      }

      return 0;
}

編譯

gcc main.c 

執行結果