c语言面向对象:列表

一个简单的列表类,源码如下:

ZListClass.h

/****************************
* Black Spoor               *
****************************/
#ifndef _BLACKSPOOR_ZLISTCLASS_H_
#define _BLACKSPOOR_ZLISTCLASS_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ZListItemClass
{
 void * content;
 int index;  //元素的序号
 struct ZListItemClass * previous;  //前一个元素
 struct ZListItemClass * next;  //下一个元素
 void (*Init)(struct ZListItemClass *,void * tag);
 void (*Dispose)(struct ZListItemClass *);
}ZListItemClass;
extern ZListItemClass * ZListItemClassCreate(void);

typedef struct ZListClass
{
 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 * (*InsertBeforeItem)(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 * (*GetItemAt)(struct ZListClass *,int index);//搜素
 struct ZListItemClass * (*SearchItem)(struct ZListClass *,struct ZListItemClass * item);//搜素
 struct ZListItemClass * (*SearchContent)(struct ZListClass * stu,void * content);//搜素
 void (*Init)(struct ZListClass *);
 void (*Dispose)(struct ZListClass *);
}ZListClass;
extern ZListClass * ZListClassCreate(void);
#endif

ZListClass.c
/****************************
* 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 void Init(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 * 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 * 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 * 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->previous = NULL;
  item->next = temp;
  temp->previous = item;
  //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;
  item->previous = NULL;
  item->next = NULL;
 }
 else
 {
  ZListItemClass * temp = stu->last;
  //temp->previous
  temp->next = item;
  item->previous = temp;
  item->next = NULL;
  //stu->first
  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;
  }
 }
}

static void Init(struct ZListClass * stu)
{
}
static void Dispose(struct ZListClass * 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;
}

static struct ZListItemClass * AppendObject(struct ZListClass * stu, void * obj)
{
 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 * 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;
  newItem->previous = NULL;
  newItem->next = oldItem;
  oldItem->previous = newItem;
 }
 else
 {
  ZListItemClass * pre = oldItem->previous;
  pre->next = newItem;
  newItem->previous = pre;
  newItem->next = oldItem;
  oldItem->previous = newItem;
 }
 stu->count ++;
 ReIndex(stu);
 return 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;
  oldItem->next = newItem;
 }
 else
 {
  ZListItemClass * next = oldItem->next;
  next->previous = newItem;
  newItem->previous = oldItem;
  newItem->next = next;
  oldItem->next = newItem;
 }
 stu->count ++;
 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;
 }
 stu->count --;
 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 * 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 * 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->AppendItem = AppendItem;
stu->AppendObject = AppendObject;
 stu->PushItem = PushItem;
 stu->PopItem = PopItem;
 stu->InsertBeforeItem = InsertBeforeItem;
 stu->InsertAfterItem = InsertAfterItem;
 stu->RemoveItem = RemoveItem;
 stu->RemoveItemAt = RemoveItemAt;
 stu->GetItemAt = GetItemAt;
 stu->SearchItem = SearchItem;
 stu->SearchContent = SearchContent;
 return stu;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值