C语言写的一个容器list
来源:互联网 发布:徐州软件行业发展前景 编辑:程序博客网 时间:2024/04/25 12:41
今天无聊用C语言写了一个容器list,我发现现在微软的vs2010已经很规范的处理C语言的编译工作了。比如说变量声明必须在函数最前面,声明一个struct变量的时候必须加上struct关键字等等。
扩展性:为了让这个list支持任意类型,list中的节点存储两个变量:一个是用户自定义的类型的对象的地址,一个是next指针。一开始我想把操作都封装起来,全部声明为静态的,然后在operations结构体中放入指向这些静态函数的指针。但是后来一想这样其实也没多大必要,因为我必须定义一个形参,这个形参为当前的list。所以干脆就把这些操作暴露给用户。
与其写出
list animals
animals.oprs.insert(&animals,&eliphant);这样的代码
还不如写出
list animals
listinsert(&animals,&eliphant);
内存管理:其实内存管理是一项非常有意思的工作,但是这里我只是简单的处理了一下,没有实现什么内存池啥的。对于内存管理当然要遵循谁申请谁释放的原则了。node是不能暴露给用户的。
遍历操作:遍历操作需要用户自己写一个回调函数实现自己想要的那套处理方法。列表的插入删除操作啥的这个仁者见仁智者见智了。我的插入操作时在链表头插入。
#ifndef LIST_H#define LIST_H#include <stdio.h>#include <malloc.h>#ifdef __cplusplusextern "C"{#endifstruct list;struct node;struct operations;/*废弃*/typedef struct node{ unsigned int value; struct node* next;}node,*nodeptr;typedef struct operations{ void (*insert)(struct list *l , void* p); void (*remove)(struct list *l , void* p); void (*traverse)(struct list *l , void (*callback)(void*));}operations,*operaptr;typedef struct list{ nodeptr head; int length;}list;void listinit(struct list* l);void listinsert(struct list *l , void* p);void listremove(struct list *l , void* p);void listtraverse(struct list *l , void(*callback)(void*));int listlength(struct list *l);static nodeptr listnewnode();#ifdef __cplusplus}#endif#endif
#include "list.h"static operations ops={ listinsert, listremove, listtraverse,};void listinit(list* l){ (*l).head=NULL; (*l).length=0;}void listinsert(list *l , void *thisnode){ nodeptr p=listnewnode(); p->value=(unsigned int)thisnode; p->next=(*l).head; (*l).head=p; (*l).length+=1;}void listremove(list *l , void *thisnode){ nodeptr head; nodeptr prev; prev=(*l).head; if(prev==NULL){ printf("This list is NULL\n"); return ; } if(thisnode==NULL){ printf("this node is NULL\n"); return ; } if(prev->value==(unsigned int)thisnode){ (*l).head=(*l).head->next; free(prev); (*l).length-=1; }else{ for(head=(*l).head ; head!=NULL ; head=head->next){ if((unsigned int)thisnode==head->value){ prev->next=head->next; free(head); (*l).length-=1; break; } prev=head; } if(head==NULL){ printf("This list has no this node\n"); } }}nodeptr listnewnode(){ return (nodeptr)malloc(sizeof(node));}/************************************************************************//* if you wanna do something to the node, please write a callback. it *//* must has the type of void(*callback)(void*) *//************************************************************************/void listtraverse(list *l , void(*callback)(void*)){ nodeptr head=(*l).head; for( ; head!=NULL ; head=head->next) callback((void*)(head->value));}int listlength(list *l){ return (*l).length;}
下面给出一个测试用的代码
#include <stdio.h>#include <stdlib.h>#include "list.h"typedef struct monkey{ int age; int master; int code;}monkey;void callback(void *param){ monkey *thismonkey=(monkey*)param; int sum=thismonkey->age+thismonkey->master+thismonkey->code; printf("%d,%d,%d,SUM:%d\n",thismonkey->age,thismonkey->master,thismonkey->code,sum);}int main(int argc ,char *argv[]){ list animals; monkey jack; monkey rose; monkey tom; monkey *p; listinit(&animals); jack.age=20; jack.master=20; jack.code=20; rose.age=21; rose.master=21; rose.code=21; tom.age=22; tom.master=22; tom.code=22; listinsert(&animals,&jack); listinsert(&animals,&rose); listinsert(&animals,&tom); listtraverse(&animals,callback); listremove(&animals,NULL); listtraverse(&animals,callback); printf("%d\n",listlength(&animals)); system("pause"); return EXIT_SUCCESS;}其实我一直都持有这种想法,C语言能实现一切的东西,因为可以直接操作内存嘛。
- C语言写的一个容器list
- C语言写的一个钟表(很炫哦)
- 用C语言写的一个万年历
- C语言写的一个病毒
- 用C语言写一个的计算器
- c语言写的一个恶意程序
- 一个C语言写的2048游戏
- 一个c语言写的文件系统
- C语言写的一个简易计算器
- C语言写的一个2048小游戏
- C语言写的一个电子时钟
- 自己写的LIST操作函数C语言
- 【C++】容器list的用法
- 【c】C语言编程写的一个http下载程序
- C语言写一个万年历
- C语言实现一个简单的单向链表list
- C语言实现一个泛型容器
- 发一个c语言写的俄罗斯方块的代码 [
- myeclipse连接数据库
- POJ1740(找规律)
- S3C6410 中断例子程序
- 在IOS中使用KeychainItemWrapper保存用户名和密码实现记住密码功能
- 对话框动态生成控件
- C语言写的一个容器list
- [leetcode]Set Matrix Zeroes
- 关于ftp中文本模式和二进制模式上传文件的区别
- 献给要毕业的大学生,毕业一年工作后。。《毕业生找工作流水帐篇 一》
- TCP/IP网络编程之四书五经
- C++---类中的静态成员
- iOS解析Server端返回JSON数据
- Linux救援模式应用
- 基础!