双向链表源代码

list.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "list.h"

LLIST *llist_create(int size)
{
        LLIST *newnode;
        newnode=malloc(sizeof(*newnode));
        if(newnode==NULL)
                return NULL;
        newnode->size=size;
        newnode->head.data=NULL;
        newnode->head.prev=&newnode->head;
        newnode->head.next=&newnode->head;
        return newnode;
}


int llist_insert(LLIST *ptr, void *data,int mode)
{
        struct llist_node_st *newnode;
        newnode=malloc(sizeof(*newnode));
        if(newnode==NULL)
                return -1;
        newnode->data=malloc(ptr->size);//give list's data mem 
        if(newnode->data==NULL)
        {
                free(newnode);
                return -1;
        }
        memcpy(newnode->data,data,ptr->size);
        if(mode==LLIST_FORWARD)
        {
                newnode->prev=&ptr->head;
                newnode->next=ptr->head.next;
        }
        else
                if(mode==LLIST_BACKWARD)
                {
                        newnode->prev=ptr->head.prev;
                        newnode->next=&ptr->head;
                }
                else
                {
                        return -2;
                }
                newnode->prev->next=newnode;
                newnode->next->prev=newnode;
                return 0;
}
static struct llist_node_st *find_(LLIST *ptr,const void *key,llist_cmp *cmp)
{
        struct llist_node_st *cur;
        for(cur=ptr->head.next;cur!=&ptr->head;cur=cur->next)
        {
                if(cmp(key,cur->data)==0)
                        break;
        }
        return cur;

}

void *llist_find(LLIST *ptr,const void *key,llist_cmp *cmp)
{

        return find_(ptr,key,cmp)->data;

}

void llist_travel(LLIST *ptr, llist_op *op)
{
        struct llist_node_st *cur;
        for(cur=ptr->head.next;cur!=&ptr->head;cur=cur->next)
        {
                op(cur->data);
        }
}

int llist_delete(LLIST *ptr,const void *key ,llist_cmp *cmp)
{
        struct llist_node_st *node;
        node=find_(ptr,key,cmp);
        if(node==&ptr->head)
                return -1; //can not find
        node->prev->next=node->next;
        node->next->prev=node->prev;

        free(node->data);
        free(node);
        return 0;
}


int llist_fetch(LLIST *ptr,const void *key,llist_cmp *cmp,void *data)  //del the key and return the del's data
{

        struct llist_node_st *node;
        node=find_(ptr,key,cmp);
        if(node==&ptr->head)
                return -1; //can not find
        memcpy(data,node->data,ptr->size);
        node->prev->next=node->next;
        node->next->prev=node->prev;


        free(node->data);
        free(node);
        return 0;
}


void llist_destory(LLIST *ptr)
{
        struct llist_node_st *cur;
        void *next;
        for(cur=ptr->head.next;cur!=&ptr->head;cur=next)
        {
                next=cur->next;
                free(cur->data);
                free(cur);
        }

}
list.h

#ifndef MY_LIST_H_
#define MY_LIST_H_

#define LLIST_FORWARD 1
#define LLIST_BACKWARD 2

typedef void llist_op(const void *);
typedef int llist_cmp(const void *,const void *);

struct llist_node_st  //link list node struct
{
        void * data;
        struct llist_node_st  *prev,*next;
};

// header pointer
typedef struct 
{
        int size;
        struct llist_node_st head;
}LLIST;

LLIST * llist_create(int size);

int llist_insert(LLIST *, void *,int);// shou bu cha ru, wei bu cha ru 2zhong fang shi

void *llist_find(LLIST *,const void *,llist_cmp *);//

int llist_delete(LLIST *,const void *,llist_cmp *);

int llist_fetch(LLIST *,const void *,llist_cmp *,void *);  //del the key and return the del's data

void llist_travel(LLIST *, llist_op *);

void llist_destory(LLIST *);



#endif

main.c

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

#define NAMESIZE 32

struct person
{
        int id;
        char name[NAMESIZE];
        int age;
};

static void print_s(const void * data)
{
        const struct person *d=(void *)data;
        printf("%d %s %d\n",d->id,d->name,d->age);
}

static int id_cmp(const void * key,const void * data)
{
        const int *k=key;
        const struct person *d=data;
        return (*k-d->id);
}

static int name_cmp(const void * key,const void * data)
{
        const char *k=key;
        const struct person *d=data;
        return ( strcmp(k,d->name));
}
int main()
{

        LLIST *handle;
        char *name="person26";
        int ret,i,id=3;
        struct person tmp,*datap;
        handle=llist_create(sizeof(struct person));
        if(handle==NULL)
                return -1;
        for( i=0;i<6;i++)
        {
                tmp.id=i;
                tmp.age=20+i;
                snprintf(tmp.name,NAMESIZE,"person%d",i);

                llist_insert(handle,&tmp,LLIST_BACKWARD);

                //llist_insert(handle,&tmp,LLIST_BACKWARD);
        }
        llist_travel(handle,print_s);
        printf("\n\n");
#if 0
        datap=llist_find(handle,name,name_cmp);

        //datap=llist_find(handle,&id,id_cmp);
        if(datap!=NULL)
                print_s(datap);
        else
                printf("can not find!\n");

        llist_delete(handle,&id,id_cmp);

        llist_travel(handle,print_s);
#endif

        ret=llist_fetch(handle,&id,id_cmp,&tmp);

        llist_travel(handle,print_s);

        printf("\n\n");
        if(ret==0)
        {
                print_s(&tmp);
        }
        llist_destory(handle);

        return 0;
}


makefile

all:main

main:main.o list.o

clean:
        rm -rf  *.o  main


源码,经典。 CARD *myinsert(LCARD *head, LCARD *insert) { LCARD *temp = NULL; if (head==NULL)//链表为空 { head = insert; insert->next = insert; insert->prior = insert; } else//链表非空 { temp = head; if (head->cardnum>insert->cardnum)//插入到头前边,并且把自己当作头 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; head = insert; } if (insert->cardnum0<50)//小于50正向插入 { while ((temp->cardnum<insert->cardnum)&&(temp->next!=head))//循环 { temp = temp->next; } if (temp->cardnum>insert->cardnum)//第一个条件终止的 { temp->prior->next = insert; insert->prior = temp->prior; insert->next = temp; temp->prior = insert; } else//第二个条件终止的 { head->prior->next = insert; insert->prior = head->prior; insert->next = head; head->prior = insert; } } else//大于50反向插入 { while ((temp->cardnum>insert->cardnum)&&(temp->prior!=head))//循环,第二个条件禁止跑飞 { temp = temp->prior; } if (temp->cardnum<insert->cardnum)//只有第一个条件可以终止的 { temp->next->prior = insert; insert->next = temp->next; insert->prior = temp; temp->next = insert; } } } //printf("%d\t%d\n", insert->id, insert->cardnum); return head; } void swap_id(SWID *sw) { LCARD *temp = sw->head; if (sw->head->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); sw->head->id = sw->inID; return ; } if ((sw->swapcardnum0)<50) { while ((temp->cardnum!=sw->swapcardnum)&&(temp->next!=sw->head)) { temp = temp->next; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } else { while ((temp->cardnum!=sw->swapcardnum)&&(temp->prior!=sw->head)) { temp = temp->prior; } if (temp->cardnum==sw->swapcardnum) { printf("out person cardnum=%d\n", sw->head->id); temp->id = sw->inID; } } } LCARD *mydel(LCARD *head, LCARD *del) { LCARD *temp = NULL; if (head==NULL)//没有链表 { printf("there is no card\n"); } else//有链表 { if(head->next==head)//链表里就有一个节点并且为头结点 { if (head->cardnum==del->cardnum) { free(head); head = NULL; } else { printf("in mydel error\n"); } } else//链表里有超过一个的节点 { temp = head; if (del->cardnum0<50)//成立则正向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->next!=head)) { temp = temp->next; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } else//反向删除 { while ((temp->cardnum!=del->cardnum)&&(temp->prior!=head)) { temp = temp->prior; } if (temp->cardnum==del->cardnum) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); } } } } return head; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值