C语言双向循环链表实现
2016-05-16 20:10
375 查看
实现思路:插入时候不是插入节点而是数据,通过自己提供的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; } }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)