C语言双向循环链表实现

实现思路:插入时候不是插入节点而是数据,通过自己提供的compare,和print函数进行特定类型的操作。而其余操作均可以定义一种通用的做法。

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

typedef struct linknode{
    void* data;
    struct linknode* prev;
    struct linknode* next;
}node;

static int init_list(node** head)
{
    *head=(node*)malloc(sizeof(node));
    if(*head==NULL){
        return -1;
    }
    (*head)->next=*head;
    (*head)->prev=*head;
    int* d=(int*)malloc(sizeof(int));
    if(d==NULL){
        return -1;
    }
    *d=0;
    (*head)->data=d;
    return 1;
}

static int list_insert_head(node* head,void* data)
{
    if(head==NULL)return 0;
    node* p=(node*)malloc(sizeof(node));
    if(p==NULL){
        return 0;
    }
    p->data=data;

    p->next=head->next;
    head->next=p;
    p->prev=head;
    p->next->prev=p;

    *((int*)(head->data))+=1;
    return 0;
}

static int list_insert_tail(node* head,void* data)
{
    if(head==NULL)return 0;
    node* p=(node*)malloc(sizeof(node));
    if(p==NULL){
        return 0;
    }
    p->data=data;

    head->prev->next=p;
    p->prev=head->prev;
    head->prev=p;
    p->next=head;
    *((int*)(head->data))+=1;

    return 0;
}

static void* list_get_head(node* head)
{
    if(head==NULL || head->next==head)return NULL;
    return head->next->data;
}

static void* list_get_tail(node* head)
{
    if(head==NULL || head->prev==head)return NULL;
    return head->prev->data;
}

static int list_delete_head(node* head)
{
    if(head==NULL || head->next==head)return 0;
    node* p=head->next;
    head->next=p->next;
    p->next->prev=head;
    free(p);
    *(int*)(head->data)-=1;
    return 1;
}

static int list_delete_tail(node* head)
{
    if(head==NULL || head->prev==head)return 0;
    node* p=head->prev;
    head->prev=p->prev;
    p->prev->next=head;
    free(p);
    *(int*)(head->data)-=1;
    return 1;
}

static void* list_at(node* head,int n)
{
    if(head==NULL)return NULL;
    int size=*(int*)head->data;
    if(n<0 || n>=size)return NULL;

    node* p=head->next;
    while(n--)p=p->next;
    return p->data;
}

static void list_sort(node *head,int (*compare)(void*,void*))
{
    int size=*(int*)head->data;
    if(size==0)return ;

    //bubble sort
    int flag=1;
    for(node* begin=head->next,*end=head->prev;begin!=end && flag==1;end=end->prev){
        for(node* index=begin;index!=end;index=index->next){
            if(compare(index->data,index->next->data)){
                void* temp=index->data;
                index->data=index->next->data;
                index->next->data=temp;
                flag=1;
            }
        }
    }
}

static int list_count(node* head)
{   
    if(head==NULL)return 0;
    return *(int*)(head->data);
}

//define by yourself according to the data type
static int list_compare_node(void* lhs,void* rhs)
{
    return *(int*)(lhs)>=*(int*)(rhs);
}

static int list_delete(node* head,void* d,int(*compare)(void*,void*))
{
    if(head==NULL)return 0;
    node* p=head->next;
    while(p!=head){
        if(compare(p->data,d)){
            p->prev->next=p->next;
            p->next->prev=p->prev;
            free(p);
            *((int*)(head->data))-=1;
            return 1;
        }
        p=p->next;
    }
    return 0;
}

static int free_list(node** head)
{
    if(*head==NULL)return 0;
    int cnt=0;
    node* p=(*head)->next;
    node* q;
    while(p!=(*head)){
        q=p->next;
        free(p);
        p=q;
        ++cnt;
    }
    free(p);
    *head=NULL;

    return cnt;
}

//define by yourself according to the data type
static void list_print_node(void* data)
{
    printf("%d\n",*(int*)data);
}

static void print_list(node* head,void(*print)(void*))
{
    if(head==NULL)return ;
    node* p=head->next;
    while(p!=head){
        print(p->data);
        p=p->next;
    }
}

static void print_list_reverse(node* head,void(*print)(void*))
{
    if(head==NULL)return ;
    node* p=head->prev;
    while(p!=head){
        print(p->data);
        p=p->prev;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值