本文发表在 rolia.net 枫下论坛Please rewrite AList defined in list.h and list.c into a C++ class. All the functions defined in list.c should be implemented as the class member functions.
/*list.h */
#ifndef _LIST_INCLUDED
#define _LIST_INCLUDED
#define ALIST_BLOCK_SIZE 100
typedef struct {
void **items;
int numOfItems;
} ALIST;
/* --- The API functions for manipulating a list --- */
#ifdef WIN32
#ifdef __cplusplus
extern "C" {
#endif
extern ALIST *IoCreateAList();
extern int IoAppendItem(ALIST *pList,void *pItem);
extern int IoInsertItem(ALIST *pList,void *pItem,int index);
extern int IoAddItem(ALIST *pList,void *pItem,int index);
extern int IoAddSortedItem(ALIST *pList,void *pItem,
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
extern void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
extern int IoRemoveItem(ALIST *pList,int index);
extern int IoDeleteItem(ALIST *pList,int index);
extern void *IoGetItem(ALIST *pList,int index);
extern int IoListSize(ALIST *pList);
extern int IoClearList(ALIST *pList);
extern int IoFreeList(ALIST *pList);
extern int IoMarkRemoveItem(ALIST *pList,int index);
extern int IoMarkDeleteItem(ALIST *pList,int index);
extern int IoSyncList(ALIST *pList);
extern int IoRemoveItemByPointer(ALIST *pList,void *pItem);
#ifdef __cplusplus
}
#endif
#else
extern ALIST *IoCreateAList();
extern int IoInsertItem();
extern int IoAddItem();
extern int IoInsertSortedItem();
extern void *IoFindSortedItem();
extern int IoAppendItem();
extern int IoRemoveItem();
extern int IoDeleteItem();
extern int IoListSize();
extern void *IoGetItem();
extern int IoClearList();
extern int IoFreeList();
extern int IoMarkRemoveItem();
extern int IoMarkDeleteItem();
extern int IoSyncList();
extern int IoRemoveItemByPointer();
#endif
#endif
===========c file ========================
/*list.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"
#include "str.h"
#include "list.h"
#define ALLOC_OFFSET 10
#define MAXLINESIZE 256
ALIST *IoCreateAList()
{
ALIST *aList;
aList=(ALIST *)malloc(sizeof(ALIST));
if(aList==NULL)
return(NULL);
aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
if (aList->items==NULL)
{
free(aList);
return(NULL);
}
aList->numOfItems=0;
return(aList);
}
int IoAppendItem(pList,pItem)
ALIST *pList;
void *pItem;
{
void **pItems;
if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
{
pItems=realloc(pList->items,
(pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items = pItems;
}
pList->items[pList->numOfItems]=pItem;
pList->numOfItems++;
return(0);
}
int IoInsertItem(pList,pItem,index)
ALIST *pList;
void *pItem;
int index;
{
int i;
void **pItems;
if (index<0 || index>pList->numOfItems)
return(-1);
else if(index==pList->numOfItems||pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
{
pItems=realloc(pList->items,
(pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items = pItems;
}
for(i=pList->numOfItems;i>index;i--)
pList->items[i] = pList->items[i-1];
pList->items[index]=pItem;
pList->numOfItems++;
return(0);
}
int IoAddItem(pList,pItem,index)
ALIST *pList;
void *pItem;
int index;
{
if(pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
else
return(IoInsertItem(pList,pItem,index+1));
}
int IoRemoveItem(pList,index)
ALIST *pList;
int index;
{
int i;
void **pItems;
if (index<0 || index>=pList->numOfItems)
return(-1);
for(i=index;i<pList->numOfItems-1;i++)
pList->items[i]=pList->items[i+1];
if(pList->numOfItems>ALIST_BLOCK_SIZE && pList->numOfItems%ALIST_BLOCK_SIZE==1)
{
pItems=realloc(pList->items,
(pList->numOfItems-1)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items=pItems;
}
pList->numOfItems--;
return(0);
}
int IoDeleteItem(pList,index)
ALIST *pList;
int index;
{
void *pItem;
if (index<0 || index>=pList->numOfItems)
return(-1);
pItem = IoGetItem(pList,index);
if(IoRemoveItem(pList,index))
return(-1);
if(pItem)
free(pItem);
return(0);
}
void* IoGetItem(pList,index)
ALIST *pList;
int index;
{
if(index>=pList->numOfItems || index<0)
return(NULL);
return(pList->items[index]);
}
int IoListSize(pList)
ALIST *pList;
{
return(pList->numOfItems);
}
int IoClearList(pList)
ALIST *pList;
{
int i, size = IoListSize(pList);
for(i=0;i<size;i++)
{
if(IoDeleteItem(pList,0))
return(-1);
}
return(0);
}
int IoFreeList(pList)
ALIST *pList;
{
free(pList->items);
free(pList);
return(0);
}
int IoMarkRemoveItem(pList,index)
ALIST *pList;
int index;
{
if(index<0||index>=pList->numOfItems)
return(-1);
pList->items[index]=NULL;
return(0);
}
int IoMarkDeleteItem(pList,index)
ALIST *pList;
int index;
{
void *pItem;
if (index<0 || index>=pList->numOfItems)
return(-1);
pItem = IoGetItem(pList,index);
if(IoMarkRemoveItem(pList,index))
return(-1);
if(pItem)
free(pItem);
return(0);
}
int IoSyncList(pList)
ALIST *pList;
{
int i,j;
for(i=0,j=0;i<pList->numOfItems;i++)
if(pList->items[i]!=NULL)
{
if(i!=j)
{
pList->items[j]=pList->items[i];
pList->items[i]=NULL;
}
j++;
}
pList->numOfItems=j;
return(0);
}
int IoRemoveItemByPointer(pList,pItem)
ALIST *pList;
void *pItem;
{
int i;
for(i=0;i<pList->numOfItems;i++)
if(pList->items[i]==pItem)
break;
if(i<pList->numOfItems)
return(IoRemoveItem(pList,i));
else
return(0);
}
int IoAddSortedItem(pList,pItem,compareTwoElements)
ALIST *pList;
void *pItem;
#ifdef WIN32
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2);
#else
int (*compareTwoElements)();
#endif
{
if(IoAppendItem(pList,pItem)<0)
return(-1);
qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements);
return(0);
}
void *IoFindSortedItem(pList,item,pos,cmpRslt,compareTwoItems)
ALIST *pList;
void *item;
int *pos,*cmpRslt;
#ifdef WIN32
int (__cdecl *compareTwoItems )(const void *item1,const void *item2);
#else
int (*compareTwoItems)();
#endif
{
int i,j,k;
void *itemInList;
if(pList->numOfItems==0)
{
*pos=0;
*cmpRslt = -1;
return(NULL);
}
i=0;
j=pList->numOfItems-1;
*pos=0;
*cmpRslt = -1;
while(1)
{
if(j<i)
{
return(NULL); /* Can not find the item */
}
k=(i+j)/2;
*pos=k;
itemInList=IoGetItem(pList,k);
if((*cmpRslt=compareTwoItems(item,itemInList))==0)
{
return(itemInList); /* The item is found */
}
if((*cmpRslt)<0)
j=k-1;
else
i=k+1;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
/*list.h */
#ifndef _LIST_INCLUDED
#define _LIST_INCLUDED
#define ALIST_BLOCK_SIZE 100
typedef struct {
void **items;
int numOfItems;
} ALIST;
/* --- The API functions for manipulating a list --- */
#ifdef WIN32
#ifdef __cplusplus
extern "C" {
#endif
extern ALIST *IoCreateAList();
extern int IoAppendItem(ALIST *pList,void *pItem);
extern int IoInsertItem(ALIST *pList,void *pItem,int index);
extern int IoAddItem(ALIST *pList,void *pItem,int index);
extern int IoAddSortedItem(ALIST *pList,void *pItem,
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
extern void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
extern int IoRemoveItem(ALIST *pList,int index);
extern int IoDeleteItem(ALIST *pList,int index);
extern void *IoGetItem(ALIST *pList,int index);
extern int IoListSize(ALIST *pList);
extern int IoClearList(ALIST *pList);
extern int IoFreeList(ALIST *pList);
extern int IoMarkRemoveItem(ALIST *pList,int index);
extern int IoMarkDeleteItem(ALIST *pList,int index);
extern int IoSyncList(ALIST *pList);
extern int IoRemoveItemByPointer(ALIST *pList,void *pItem);
#ifdef __cplusplus
}
#endif
#else
extern ALIST *IoCreateAList();
extern int IoInsertItem();
extern int IoAddItem();
extern int IoInsertSortedItem();
extern void *IoFindSortedItem();
extern int IoAppendItem();
extern int IoRemoveItem();
extern int IoDeleteItem();
extern int IoListSize();
extern void *IoGetItem();
extern int IoClearList();
extern int IoFreeList();
extern int IoMarkRemoveItem();
extern int IoMarkDeleteItem();
extern int IoSyncList();
extern int IoRemoveItemByPointer();
#endif
#endif
===========c file ========================
/*list.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "args.h"
#include "str.h"
#include "list.h"
#define ALLOC_OFFSET 10
#define MAXLINESIZE 256
ALIST *IoCreateAList()
{
ALIST *aList;
aList=(ALIST *)malloc(sizeof(ALIST));
if(aList==NULL)
return(NULL);
aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
if (aList->items==NULL)
{
free(aList);
return(NULL);
}
aList->numOfItems=0;
return(aList);
}
int IoAppendItem(pList,pItem)
ALIST *pList;
void *pItem;
{
void **pItems;
if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
{
pItems=realloc(pList->items,
(pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items = pItems;
}
pList->items[pList->numOfItems]=pItem;
pList->numOfItems++;
return(0);
}
int IoInsertItem(pList,pItem,index)
ALIST *pList;
void *pItem;
int index;
{
int i;
void **pItems;
if (index<0 || index>pList->numOfItems)
return(-1);
else if(index==pList->numOfItems||pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
if(pList->numOfItems>0 && pList->numOfItems%ALIST_BLOCK_SIZE==0)
{
pItems=realloc(pList->items,
(pList->numOfItems+ALIST_BLOCK_SIZE)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items = pItems;
}
for(i=pList->numOfItems;i>index;i--)
pList->items[i] = pList->items[i-1];
pList->items[index]=pItem;
pList->numOfItems++;
return(0);
}
int IoAddItem(pList,pItem,index)
ALIST *pList;
void *pItem;
int index;
{
if(pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
else
return(IoInsertItem(pList,pItem,index+1));
}
int IoRemoveItem(pList,index)
ALIST *pList;
int index;
{
int i;
void **pItems;
if (index<0 || index>=pList->numOfItems)
return(-1);
for(i=index;i<pList->numOfItems-1;i++)
pList->items[i]=pList->items[i+1];
if(pList->numOfItems>ALIST_BLOCK_SIZE && pList->numOfItems%ALIST_BLOCK_SIZE==1)
{
pItems=realloc(pList->items,
(pList->numOfItems-1)*sizeof(void*)
);
if(pItems==NULL)
return(-1);
pList->items=pItems;
}
pList->numOfItems--;
return(0);
}
int IoDeleteItem(pList,index)
ALIST *pList;
int index;
{
void *pItem;
if (index<0 || index>=pList->numOfItems)
return(-1);
pItem = IoGetItem(pList,index);
if(IoRemoveItem(pList,index))
return(-1);
if(pItem)
free(pItem);
return(0);
}
void* IoGetItem(pList,index)
ALIST *pList;
int index;
{
if(index>=pList->numOfItems || index<0)
return(NULL);
return(pList->items[index]);
}
int IoListSize(pList)
ALIST *pList;
{
return(pList->numOfItems);
}
int IoClearList(pList)
ALIST *pList;
{
int i, size = IoListSize(pList);
for(i=0;i<size;i++)
{
if(IoDeleteItem(pList,0))
return(-1);
}
return(0);
}
int IoFreeList(pList)
ALIST *pList;
{
free(pList->items);
free(pList);
return(0);
}
int IoMarkRemoveItem(pList,index)
ALIST *pList;
int index;
{
if(index<0||index>=pList->numOfItems)
return(-1);
pList->items[index]=NULL;
return(0);
}
int IoMarkDeleteItem(pList,index)
ALIST *pList;
int index;
{
void *pItem;
if (index<0 || index>=pList->numOfItems)
return(-1);
pItem = IoGetItem(pList,index);
if(IoMarkRemoveItem(pList,index))
return(-1);
if(pItem)
free(pItem);
return(0);
}
int IoSyncList(pList)
ALIST *pList;
{
int i,j;
for(i=0,j=0;i<pList->numOfItems;i++)
if(pList->items[i]!=NULL)
{
if(i!=j)
{
pList->items[j]=pList->items[i];
pList->items[i]=NULL;
}
j++;
}
pList->numOfItems=j;
return(0);
}
int IoRemoveItemByPointer(pList,pItem)
ALIST *pList;
void *pItem;
{
int i;
for(i=0;i<pList->numOfItems;i++)
if(pList->items[i]==pItem)
break;
if(i<pList->numOfItems)
return(IoRemoveItem(pList,i));
else
return(0);
}
int IoAddSortedItem(pList,pItem,compareTwoElements)
ALIST *pList;
void *pItem;
#ifdef WIN32
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2);
#else
int (*compareTwoElements)();
#endif
{
if(IoAppendItem(pList,pItem)<0)
return(-1);
qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements);
return(0);
}
void *IoFindSortedItem(pList,item,pos,cmpRslt,compareTwoItems)
ALIST *pList;
void *item;
int *pos,*cmpRslt;
#ifdef WIN32
int (__cdecl *compareTwoItems )(const void *item1,const void *item2);
#else
int (*compareTwoItems)();
#endif
{
int i,j,k;
void *itemInList;
if(pList->numOfItems==0)
{
*pos=0;
*cmpRslt = -1;
return(NULL);
}
i=0;
j=pList->numOfItems-1;
*pos=0;
*cmpRslt = -1;
while(1)
{
if(j<i)
{
return(NULL); /* Can not find the item */
}
k=(i+j)/2;
*pos=k;
itemInList=IoGetItem(pList,k);
if((*cmpRslt=compareTwoItems(item,itemInList))==0)
{
return(itemInList); /* The item is found */
}
if((*cmpRslt)<0)
j=k-1;
else
i=k+1;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net