一个简单的列表类,源码如下:
ZListClass.h
/****************************
* Black Spoor *
****************************/
* Black Spoor *
****************************/
#ifndef _BLACKSPOOR_ZLISTCLASS_H_
#define _BLACKSPOOR_ZLISTCLASS_H_
#define _BLACKSPOOR_ZLISTCLASS_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
typedef struct ZListItemClass
{
void * content;
int index; //元素的序号
{
void * content;
int index; //元素的序号
struct ZListItemClass * previous; //前一个元素
struct ZListItemClass * next; //下一个元素
struct ZListItemClass * next; //下一个元素
void (*Init)(struct ZListItemClass *,void * tag);
void (*Dispose)(struct ZListItemClass *);
void (*Dispose)(struct ZListItemClass *);
}ZListItemClass;
extern ZListItemClass * ZListItemClassCreate(void);
typedef struct ZListClass
{
int count; //元素的数量
{
int count; //元素的数量
struct ZListItemClass * first; //第一个元素
struct ZListItemClass * last; //最后一个元素
struct ZListItemClass * (*AppendItem)(struct ZListClass *,struct ZListItemClass *); //添加到末尾
struct ZListItemClass * (*AppendObject)(struct ZListClass *, void *);
struct ZListItemClass * (*PushItem)(struct ZListClass *, struct ZListItemClass * item);//添加到开始
struct ZListItemClass * (*PopItem)(struct ZListClass *);从末尾移除
struct ZListItemClass * (*PopItem)(struct ZListClass *);从末尾移除
struct ZListItemClass * (*InsertBeforeItem)(struct ZListClass *,struct ZListItemClass * newItem,struct ZListItemClass * oldItem);//在之前添加
struct ZListItemClass * (*InsertAfterItem)(struct ZListClass *,struct ZListItemClass * newItem,struct ZListItemClass * oldItem);//在之后添加
struct ZListItemClass * (*InsertAfterItem)(struct ZListClass *,struct ZListItemClass * newItem,struct ZListItemClass * oldItem);//在之后添加
struct ZListItemClass * (*RemoveItem)(struct ZListClass *,struct ZListItemClass * item);//移除
struct ZListItemClass * (*RemoveItemAt)(struct ZListClass *,int index);//移除
struct ZListItemClass * (*RemoveItemAt)(struct ZListClass *,int index);//移除
struct ZListItemClass * (*GetItemAt)(struct ZListClass *,int index);//搜素
struct ZListItemClass * (*SearchItem)(struct ZListClass *,struct ZListItemClass * item);//搜素
struct ZListItemClass * (*SearchContent)(struct ZListClass * stu,void * content);//搜素
struct ZListItemClass * (*SearchItem)(struct ZListClass *,struct ZListItemClass * item);//搜素
struct ZListItemClass * (*SearchContent)(struct ZListClass * stu,void * content);//搜素
void (*Init)(struct ZListClass *);
void (*Dispose)(struct ZListClass *);
void (*Dispose)(struct ZListClass *);
}ZListClass;
extern ZListClass * ZListClassCreate(void);
#endif
ZListClass.c
/****************************
* Black Spoor *
****************************/
* Black Spoor *
****************************/
#include "ZListClass.h"
static struct ZListItemClass * AddToFirstItem(struct ZListClass *,struct ZListItemClass *);
static struct ZListItemClass * AddToLastItem(struct ZListClass *,struct ZListItemClass *);
static void ReIndex(struct ZListClass *);
static struct ZListItemClass * AddToLastItem(struct ZListClass *,struct ZListItemClass *);
static void ReIndex(struct ZListClass *);
static void Init(struct ZListClass * stu);
static void Dispose(struct ZListClass * stu);
static void Dispose(struct ZListClass * stu);
static struct ZListItemClass * AppendItem(struct ZListClass *,struct ZListItemClass *);
static struct ZListItemClass * AppendObject(struct ZListClass *, void *);
static struct ZListItemClass * PushItem(struct ZListClass *,struct ZListItemClass *);
static struct ZListItemClass * PopItem(struct ZListClass *);
static struct ZListItemClass * PushItem(struct ZListClass *,struct ZListItemClass *);
static struct ZListItemClass * PopItem(struct ZListClass *);
static struct ZListItemClass * InsertBeforeItem(struct ZListClass *,struct ZListItemClass * newItem,struct ZListItemClass * oldItem);
static struct ZListItemClass * InsertAfterItem(struct ZListClass *,struct ZListItemClass * newItem,struct ZListItemClass * oldItem);
static struct ZListItemClass * RemoveItem(struct ZListClass *,struct ZListItemClass * item);
static struct ZListItemClass * RemoveItemAt(struct ZListClass *,int index);
static struct ZListItemClass * RemoveItemAt(struct ZListClass *,int index);
static struct ZListItemClass * GetItemAt(struct ZListClass *,int index);
static struct ZListItemClass * SearchItem(struct ZListClass *,struct ZListItemClass * item);
static struct ZListItemClass * SearchContent(struct ZListClass * stu,void * content);
static struct ZListItemClass * SearchItem(struct ZListClass *,struct ZListItemClass * item);
static struct ZListItemClass * SearchContent(struct ZListClass * stu,void * content);
static struct ZListItemClass * AddToFirstItem(struct ZListClass * stu,struct ZListItemClass * item)
{
if(stu->first == NULL)
{
stu->first = item;
stu->last = item;
item->previous = NULL;
item->next = NULL;
}
else
{
ZListItemClass * temp = stu->first;
item->next = NULL;
}
else
{
ZListItemClass * temp = stu->first;
item->previous = NULL;
item->next = temp;
item->next = temp;
temp->previous = item;
//temp->next
//temp->next
stu->first = item;
}
}
return item;
}
}
static struct ZListItemClass * AddToLastItem(struct ZListClass * stu,struct ZListItemClass * item)
{
if(stu->first == NULL)
{
stu->first = item;
stu->last = item;
{
if(stu->first == NULL)
{
stu->first = item;
stu->last = item;
item->previous = NULL;
item->next = NULL;
}
else
{
ZListItemClass * temp = stu->last;
item->next = NULL;
}
else
{
ZListItemClass * temp = stu->last;
//temp->previous
temp->next = item;
temp->next = item;
item->previous = temp;
item->next = NULL;
item->next = NULL;
//stu->first
stu->last = item;
}
stu->last = item;
}
return item;
}
}
static void ReIndex(struct ZListClass * stu)
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
temp->index = i;
temp = temp->next;
}
}
}
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
temp->index = i;
temp = temp->next;
}
}
}
static void Init(struct ZListClass * stu)
{
}
static void Dispose(struct ZListClass * stu)
{
if(stu != NULL)
{
free(stu);
}
}
{
if(stu != NULL)
{
free(stu);
}
}
//添加到末尾
static struct ZListItemClass * AppendItem(struct ZListClass * stu,struct ZListItemClass * item)
{
ZListItemClass * temp = AddToLastItem(stu,item);
stu->count ++;
ReIndex(stu);
return temp;
}
{
ZListItemClass * item = ZListItemClassCreate();
item->Init(item,obj);
//item->content = obj;
return AppendItem(stu,item);
}
//添加到开始
static struct ZListItemClass * PushItem(struct ZListClass * stu,struct ZListItemClass * item)
{
ZListItemClass * temp = AddToFirstItem(stu,item);
stu->count ++;
ReIndex(stu);
return temp;
}
static struct ZListItemClass * PushItem(struct ZListClass * stu,struct ZListItemClass * item)
{
ZListItemClass * temp = AddToFirstItem(stu,item);
stu->count ++;
ReIndex(stu);
return temp;
}
//从末尾移除
static struct ZListItemClass * PopItem(struct ZListClass * stu)
{
return RemoveItem(stu, stu->last);
}
static struct ZListItemClass * PopItem(struct ZListClass * stu)
{
return RemoveItem(stu, stu->last);
}
//在之前添加
static struct ZListItemClass * InsertBeforeItem(struct ZListClass * stu,struct ZListItemClass * newItem,struct ZListItemClass * oldItem)
{
if(oldItem->previous == NULL)
{
//插到第一个
stu->first = newItem;
static struct ZListItemClass * InsertBeforeItem(struct ZListClass * stu,struct ZListItemClass * newItem,struct ZListItemClass * oldItem)
{
if(oldItem->previous == NULL)
{
//插到第一个
stu->first = newItem;
newItem->previous = NULL;
newItem->next = oldItem;
newItem->next = oldItem;
oldItem->previous = newItem;
}
else
{
ZListItemClass * pre = oldItem->previous;
}
else
{
ZListItemClass * pre = oldItem->previous;
pre->next = newItem;
newItem->previous = pre;
newItem->next = oldItem;
newItem->next = oldItem;
oldItem->previous = newItem;
}
}
stu->count ++;
ReIndex(stu);
return newItem;
}
ReIndex(stu);
return newItem;
}
//在之后添加
static struct ZListItemClass * InsertAfterItem(struct ZListClass * stu,struct ZListItemClass * newItem,struct ZListItemClass * oldItem)
{
if(oldItem->next == NULL)
{
//插到最后
stu->last = newItem;
static struct ZListItemClass * InsertAfterItem(struct ZListClass * stu,struct ZListItemClass * newItem,struct ZListItemClass * oldItem)
{
if(oldItem->next == NULL)
{
//插到最后
stu->last = newItem;
newItem->previous = oldItem;
newItem->next = NULL;
newItem->next = NULL;
oldItem->next = newItem;
}
else
{
ZListItemClass * next = oldItem->next;
}
else
{
ZListItemClass * next = oldItem->next;
next->previous = newItem;
newItem->previous = oldItem;
newItem->next = next;
newItem->next = next;
oldItem->next = newItem;
}
}
stu->count ++;
ReIndex(stu);
return newItem;
}
ReIndex(stu);
return newItem;
}
//移除
static struct ZListItemClass * RemoveItem(struct ZListClass * stu,struct ZListItemClass * item)
{
if(item->previous == NULL && item->next == NULL)
{
stu->first = NULL;
stu->last = NULL;
}
else
{
if(item->previous == NULL)
{
//移除第一个
stu->first = item->next;
stu->first->previous = NULL;
}
else if(item->next == NULL)
{
//移除最后一个
stu->last = item->previous;
stu->last->next = NULL;
}
else
{
//移除中间项
item->previous->next = item->next;
item->next->previous = item->previous;
}
item->previous = NULL;
item->next = NULL;
}
item->next = NULL;
}
stu->count --;
ReIndex(stu);
return item;
}
ReIndex(stu);
return item;
}
//移除
static struct ZListItemClass * RemoveItemAt(struct ZListClass * stu,int index)
{
ZListItemClass * temp = stu->GetItemAt(stu,index);
if(temp!= NULL)
{
temp = stu->RemoveItem(stu,temp);
}
return temp;
}
static struct ZListItemClass * RemoveItemAt(struct ZListClass * stu,int index)
{
ZListItemClass * temp = stu->GetItemAt(stu,index);
if(temp!= NULL)
{
temp = stu->RemoveItem(stu,temp);
}
return temp;
}
//搜素
static struct ZListItemClass * GetItemAt(struct ZListClass * stu,int index)
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(i == index)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
static struct ZListItemClass * GetItemAt(struct ZListClass * stu,int index)
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(i == index)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
//搜素
static struct ZListItemClass * SearchItem(struct ZListClass * stu,struct ZListItemClass * item)
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(temp == item)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
static struct ZListItemClass * SearchItem(struct ZListClass * stu,struct ZListItemClass * item)
{
int i=0;
if(stu->count >0)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(temp == item)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
//搜素
static struct ZListItemClass * SearchContent(struct ZListClass * stu,void * content)
{
int i=0;
if(stu->count >0 && content!= NULL)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(temp->content == content)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
static struct ZListItemClass * SearchContent(struct ZListClass * stu,void * content)
{
int i=0;
if(stu->count >0 && content!= NULL)
{
ZListItemClass * temp = stu->first;
for(i=0;i<stu->count;++i)
{
if(temp->content == content)
{
return temp;
}
temp = temp->next;
}
}
return NULL;
}
ZListClass * ZListClassCreate(void)
{
ZListClass * stu = (ZListClass *)malloc(sizeof(ZListClass));
stu->Init = Init;
stu->Dispose = Dispose;
stu->Dispose = Dispose;
stu->AppendItem = AppendItem;
stu->AppendObject = AppendObject;
stu->PushItem = PushItem;
stu->PopItem = PopItem;
stu->PushItem = PushItem;
stu->PopItem = PopItem;
stu->InsertBeforeItem = InsertBeforeItem;
stu->InsertAfterItem = InsertAfterItem;
stu->InsertAfterItem = InsertAfterItem;
stu->RemoveItem = RemoveItem;
stu->RemoveItemAt = RemoveItemAt;
stu->RemoveItemAt = RemoveItemAt;
stu->GetItemAt = GetItemAt;
stu->SearchItem = SearchItem;
stu->SearchContent = SearchContent;
stu->SearchItem = SearchItem;
stu->SearchContent = SearchContent;
return stu;
}
}