【数据结构】之内核链表

#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;
                }
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值