C语言链表库(gcc、mingw编译通过,不支持VC6(因为VC6不支持变参函数))
来源:互联网 发布:php简单计算器代码 编辑:程序博客网 时间:2024/06/06 10:52
头文件:
LinkedList.h
/************************************* * * 作者:陈杰柱 * * 2017年11月 * * * * * *************************************/ /**防止重复引用**/ #ifndef _LINKEDLIST_H#define _LINKEDLIST_H#endif#define new(Class) (Class*)malloc(sizeof(Class))#define findMethodByObject(Object, Method, ...) Object->Method(Object, __VA_ARGS__)#ifdef __cplusplusextern "C" {#endiftypedef struct LinkedListItem ListItem;typedef struct LinkedListHead ListHead;struct LinkedListItem{void* content;ListItem *prev, *next;};struct LinkedListHead{ListItem *headItem;ListItem *lastItem;int length;void (*removeItem)(ListHead* head, ListItem* item);void (*removeItemByPosition)(ListHead* head, int pos);void (*add)(ListHead* head, void* content);void* (*get)(ListHead* head, int pos);void (*insert)(ListHead* head, void* content, int pos);};void removeItem(ListHead* head, ListItem* item);void removeItemByPosition(ListHead* head, int pos);void add(ListHead* head, void* content);void* get(ListHead* head, int pos);void insert(ListHead* head, void* content, int pos);#ifdef __cplusplus}#endif#include "LinkedList.c"
实现文件:
LinkedList.c
/************************************* * * 作者:陈杰柱 * * 2017年11月 * * * * * *************************************/ void removeItem(ListHead* head, ListItem* item){ListItem *cursor = head->headItem;if(item == NULL) return;/**如果要删除的是头结点**/if(head->headItem == item){head->headItem = head->headItem->next;head->headItem->prev = NULL;if(cursor->content != NULL)free(cursor->content);free(cursor);head->length --;return;}/**如果要删除的是尾结点**/if(head->lastItem == item){head->lastItem = head->lastItem->prev;head->lastItem->next = NULL;if(item->content != NULL)free(item->content);free(item);head->length --;return; }/**其他结点**/while(cursor != NULL){if(cursor == item){cursor->next->prev = cursor->prev;cursor->prev->next = cursor->next; if(item->content != NULL)free(item->content);free(item);head->length --;return;}cursor = cursor->next;}}void removeItemByPosition(ListHead* head, int pos){ListItem *cursor = head->headItem;/**如果要删除的是头结点**/if(pos == 0){head->headItem = head->headItem->next;head->headItem->prev = NULL;if(cursor->content != NULL)free(cursor->content);free(cursor);head->length --;return;}/**如果要删除的是尾结点**/if(pos == head->length - 1){head->lastItem = head->lastItem->prev;head->lastItem->next = NULL;if(cursor->content != NULL)free(cursor->content);free(cursor);head->length --;return; }/**其他结点**/for(; pos > 0; pos--){if(cursor == NULL)return ;cursor = cursor->next;}cursor->next->prev = cursor->prev;cursor->prev->next = cursor->next;if(cursor->content != NULL)free(cursor->content);free(cursor);head->length --;return;}void add(ListHead* head, void* content){if(content == NULL) return;/**如果表中没有项就新建第一项**/if(head->headItem == NULL){ head->headItem = new(ListItem);head->lastItem = head->headItem;head->headItem->prev = NULL;head->headItem->next = NULL;head->headItem->content = content;}else{ ListItem* newItem = new(ListItem);/**新加项的前序地址是当前列表最后一项**/newItem->prev = head->lastItem;newItem->next = NULL;newItem->content = content;/**新加项成为列表最后一项,之前的最后一项成为倒数第二项**/ head->lastItem->next = newItem;head->lastItem = newItem;}head->length ++;}void* get(ListHead* head, int pos){ListItem *cursor = head->headItem;for(; pos > 0; pos--){if(cursor == NULL)return NULL;cursor = cursor->next;}return cursor->content;}void insert(ListHead* head, void* content, int pos){ListItem *cursor = head->headItem;/**如果插入到第一项**/if(pos == 0){if(head != NULL){ListItem* newItem = new(ListItem);newItem->content = content;newItem->prev = NULL; head->headItem->prev = newItem;newItem->next = head->headItem;head->headItem = newItem; head->length ++;return; }}/**如果插入到最后一项**/ if(pos == head->length - 1){ if(head != NULL){ ListItem* newItem = new(ListItem);newItem->content = content;newItem->next = NULL;head->lastItem->next = newItem;newItem->prev = head->lastItem;head->lastItem = newItem; head->length ++;return;} } /**其他**/ for(; pos > 1; pos--){if(cursor == NULL)return ;cursor = cursor->next;}{ListItem* newItem = new(ListItem);newItem->content = content;newItem->next = cursor->next;newItem->prev = cursor;cursor->next->prev = newItem;cursor->next = newItem;head->length ++;}}void destory(ListHead* head){if(head == NULL) return;ListItem* cursor = head->headItem;ListItem* next = NULL;int i=0;while(cursor != NULL){next = cursor->next;if(cursor->content != NULL)free(cursor->content);cursor->content = NULL;free(cursor);cursor = NULL;cursor = next;//printf("clear:%d\n", i++);}free(head);//printf("done\n");}ListHead* createList(){ListHead* head = new(ListHead);/**新条目没有内容**/ head->headItem = NULL;head->length = 0; head->removeItem = removeItem;head->removeItemByPosition = removeItemByPosition;head->add = add;head->get = get;head->insert = insert;return head;}
使用例子:
#include "stdio.h"#include "stdlib.h"#include "LinkedList.h"int main(){int i;ListHead* list = createList();/**添项展示**/ for(i = 0; i < 100; i++){int* content = new(int);*content = i;findMethodByObject(list, add, (void*)content);}/**减项展示**/ findMethodByObject(list, removeItem, list->headItem->next);findMethodByObject(list, removeItem, list->headItem->next);findMethodByObject(list, removeItem, list->lastItem);printf("list length:%d\n", list->length);findMethodByObject(list, removeItemByPosition, 0);printf("list length:%d\n", list->length);{int* content = new(int);*content = 9988;findMethodByObject(list, insert, content, 90);}*(int*)findMethodByObject(list, get, 91) = 8899;for(i = 0; i < list->length; i++){printf("%d\t", *((int*)findMethodByObject(list, get, i)));}destory(list);printf("done\n");return 0;}
阅读全文
0 0
- C语言链表库(gcc、mingw编译通过,不支持VC6(因为VC6不支持变参函数))
- __FUNCTION__ 在C语言中以编程的方式获取函数名(VC6.0不支持)
- VC6不支持这样的函数模板吗????
- vc6不支持long long
- C/C++宏定义的可变参数(VC6不支持可变参数宏)
- VC6不支持的模板特性
- VC6不支持强类型枚举
- VC6不支持模板函数在cpp文件中的实现
- VC6不支持模板函数在cpp文件中的实现
- VC6.0不支持unbound friend class(没有绑定的友元类)
- WIN7家庭版 是不是不支持VC6.0啊
- c语言不支持函数引用传递参数
- 定时关机程序(恶搞版)---通过VC6.0+C语言+windows API实现
- VC6下不支持函数模板显式实例化的解决方案
- VC6下不支持函数模板显式实例化的解决方案
- VC6/VC2005均不支持行数超过65536的C/C++源代码文件
- gcc不支持标准c的gets与puts函数
- gcc不支持标准c的gets puts函数
- 区分几个概念
- 织梦5.7的一些小修改
- java中ajax的用法
- 《20171124》
- 利用 webpack 处理开发与线上环境静态资源切换问题
- C语言链表库(gcc、mingw编译通过,不支持VC6(因为VC6不支持变参函数))
- 为什么Base64编码有回车换行符
- PAT 天梯赛 L1-027. 出租 (简单字符串处理)
- 错误error C1083: 无法打开包括文件: “gpu/mxGPUArray.h”: No such file or directory
- Java开发练习8 多线程
- android 设置 永不休眠
- webpack2利用插件clean-webpack-plugin来清除dist文件夹中重复的文件
- 三目运算符详解
- Android 使用 WindowManager 打造悬浮通知