本文发表在 rolia.net 枫下论坛#ifndef _MYLIST_INCLUDED
#define _MYLIST_INCLUDED
const int ALIST_BLOCK_SIZE = 100
class ALIST {
public:
void **items;
int numOfItems;
} ;
class MyList
{
public:
ALIST *IoCreateAList();
int IoAppendItem(ALIST *pList,void *pItem);
int IoInsertItem(ALIST *pList,void *pItem,int index);
int IoAddItem(ALIST *pList,void *pItem,int index);
int IoAddSortedItem(ALIST *pList,void *pItem,
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
int IoRemoveItem(ALIST *pList,int index);
int IoDeleteItem(ALIST *pList,int index);
void *IoGetItem(ALIST *pList,int index);
int IoListSize(ALIST *pList);
int IoClearList(ALIST *pList);
int IoFreeList(ALIST *pList);
int IoMarkRemoveItem(ALIST *pList,int index);
int IoMarkDeleteItem(ALIST *pList,int index);
int IoSyncList(ALIST *pList);
int IoRemoveItemByPointer(ALIST *pList,void *pItem);
};
#endif
=========cpp file====================
#include "MyList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int ALLOC_OFFSET =10;
const int MAXLINESIZE =256;
Class ALIST;
ALIST * MyList::IoCreateAList()
{
ALIST *aList;
aList=(ALIST *)new ALIST();
if(aList==NULL)
return(NULL);
aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
if (aList->items==NULL)
{
delete aList;
return(NULL);
}
aList->numOfItems=0;
return(aList);
}
int MyList::IoAppendItem(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 MyList::IoInsertItem(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 MyList::IoAddItem(ALIST *pList,void* pItem,int index)
{
if(pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
else
return(IoInsertItem(pList,pItem,index+1));
}
int MyList::IoRemoveItem(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 MyList::IoDeleteItem(ALIST* pList,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* MyList::IoGetItem(ALIST* pList,int index)
{
if(index>=pList->numOfItems || index<0)
return(NULL);
return(pList->items[index]);
}
int MyList::IoListSize(ALIST* pList)
{
return(pList->numOfItems);
}
int MyList::IoClearList(ALIST* pList)
{
int i, size = IoListSize(pList);
for(i=0;i<size;i++)
{
if(IoDeleteItem(pList,0))
return(-1);
}
return(0);
}
int MyList::IoFreeList(ALIST* pList)
{
free(pList->items);
free(pList);
return(0);
}
int MyList::IoMarkRemoveItem(ALIST* pList,int index)
{
if(index<0||index>=pList->numOfItems)
return(-1);
pList->items[index]=NULL;
return(0);
}
int MyList::IoMarkDeleteItem(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 MyList::IoSyncList(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 MyList::IoRemoveItemByPointer(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 MyList::IoAddSortedItem(ALIST *pList,void* pItem,int __cdecl* compareTwoElements(const void **elem1,const void **elem2))
{
if(IoAppendItem(pList,pItem)<0)
return(-1);
qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements(elem1, elem2));
return(0);
}
void *MyList::IoFindSortedItem(ALIST* pList,void* item,int* pos,int* cmpRslt,int (__cdecl *compareTwoItems )(const void *item1,const void *item2))
{
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
#define _MYLIST_INCLUDED
const int ALIST_BLOCK_SIZE = 100
class ALIST {
public:
void **items;
int numOfItems;
} ;
class MyList
{
public:
ALIST *IoCreateAList();
int IoAppendItem(ALIST *pList,void *pItem);
int IoInsertItem(ALIST *pList,void *pItem,int index);
int IoAddItem(ALIST *pList,void *pItem,int index);
int IoAddSortedItem(ALIST *pList,void *pItem,
int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2));
void *IoFindSortedItem(ALIST *pList,void *item, int *pos, int *cmpRslt,
int (__cdecl *compareTwoItems)(const void *item1,const void *item2));
int IoRemoveItem(ALIST *pList,int index);
int IoDeleteItem(ALIST *pList,int index);
void *IoGetItem(ALIST *pList,int index);
int IoListSize(ALIST *pList);
int IoClearList(ALIST *pList);
int IoFreeList(ALIST *pList);
int IoMarkRemoveItem(ALIST *pList,int index);
int IoMarkDeleteItem(ALIST *pList,int index);
int IoSyncList(ALIST *pList);
int IoRemoveItemByPointer(ALIST *pList,void *pItem);
};
#endif
=========cpp file====================
#include "MyList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int ALLOC_OFFSET =10;
const int MAXLINESIZE =256;
Class ALIST;
ALIST * MyList::IoCreateAList()
{
ALIST *aList;
aList=(ALIST *)new ALIST();
if(aList==NULL)
return(NULL);
aList->items=malloc(ALIST_BLOCK_SIZE*sizeof(void*));
if (aList->items==NULL)
{
delete aList;
return(NULL);
}
aList->numOfItems=0;
return(aList);
}
int MyList::IoAppendItem(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 MyList::IoInsertItem(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 MyList::IoAddItem(ALIST *pList,void* pItem,int index)
{
if(pList->numOfItems==0)
return(IoAppendItem(pList,pItem));
else
return(IoInsertItem(pList,pItem,index+1));
}
int MyList::IoRemoveItem(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 MyList::IoDeleteItem(ALIST* pList,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* MyList::IoGetItem(ALIST* pList,int index)
{
if(index>=pList->numOfItems || index<0)
return(NULL);
return(pList->items[index]);
}
int MyList::IoListSize(ALIST* pList)
{
return(pList->numOfItems);
}
int MyList::IoClearList(ALIST* pList)
{
int i, size = IoListSize(pList);
for(i=0;i<size;i++)
{
if(IoDeleteItem(pList,0))
return(-1);
}
return(0);
}
int MyList::IoFreeList(ALIST* pList)
{
free(pList->items);
free(pList);
return(0);
}
int MyList::IoMarkRemoveItem(ALIST* pList,int index)
{
if(index<0||index>=pList->numOfItems)
return(-1);
pList->items[index]=NULL;
return(0);
}
int MyList::IoMarkDeleteItem(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 MyList::IoSyncList(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 MyList::IoRemoveItemByPointer(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 MyList::IoAddSortedItem(ALIST *pList,void* pItem,int __cdecl* compareTwoElements(const void **elem1,const void **elem2))
{
if(IoAppendItem(pList,pItem)<0)
return(-1);
qsort(pList->items,pList->numOfItems,sizeof(void*),compareTwoElements(elem1, elem2));
return(0);
}
void *MyList::IoFindSortedItem(ALIST* pList,void* item,int* pos,int* cmpRslt,int (__cdecl *compareTwoItems )(const void *item1,const void *item2))
{
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