【數據結構】之內核鏈表

#include <string.h>
struct list_head{
        struct list_head *next,*pre;
};
#define LIST_HEAD(name) struct list_head name = {&(name),&(name)} 
void __list_add(struct list_head *new,struct list_head *pre,struct list_head *next)
{       
        new->next = next;
        next->pre = new;
        new->pre = pre;
        pre->next = new;
}
void list_add(struct list_head *head,struct list_head *new)
{
        __list_add(new,head,head->next);
}
void list_add_tail(struct list_head *head,struct list_head *new)
{
        __list_add(new,head->pre,head);
}
/*****************************del*****************************/
void __list_del(struct list_head *pre,struct list_head *next)
{
        pre->next = next;
        next->pre = pre;
}
void list_del(struct list_head *entry)
{
        __list_del(entry->pre,entry->next);
}
/************************************************************/
#define list_for_entry(ptr,type,member)\
        (type*)((char*)ptr-(unsigned long)&(((type*)0)->member))
#define list_for_each(pos,head) \
        for(pos = (head)->next; pos != head; pos = pos->next )
struct animal {
        char name[100];
        struct list_head animal_head;
};
LIST_HEAD(animal_head);
void add_animal(char *na)
{
        struct animal *new = (struct animal*)malloc(sizeof(struct animal));
        if(new == NULL) {
                printf("malloc error...\n");
                return;
        }
        strcpy(new->name,na);
        list_add_tail(&animal_head,&new->animal_head);
}
void del_animal(char *na)
{
        struct animal *tmp;
        struct list_head *tmp_head;
        list_for_each(tmp_head,&animal_head) {
                tmp = list_for_entry(tmp_head,struct animal,animal_head);
                if(!(strcmp(tmp->name,na))){
                        printf("find the name:%s\n",tmp->name);
                        list_del(&tmp->animal_head);
                }
        }
}
void list_animal()
{
        struct animal *tmp;
        struct list_head *tmp_head;
        list_for_each(tmp_head,&animal_head) {
                tmp = list_for_entry(tmp_head,struct animal,animal_head);
                printf("find the name:%s\n",tmp->name);
        }
}
int main()
{
        char buf[100];
        char cmd;
        while(1){
                printf("please intput a:[add],d:[delete],f[list] \n");
                scanf("%c",&cmd);
                switch (cmd){
                case 'a':
                        printf("please input name to add:\n\r");
                        scanf("%s",buf);
                        add_animal(buf);
                        break;
                case 'd':
                        printf("please input name to del:\n\r");
                        scanf("%s",buf);
                        del_animal(buf);
                        break;
                case 'f':
                        printf("begin to list:\n");
                        list_animal();
                        break;
                }
        }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章