(讨论发分100!)用C++的类(class)重新实现在list.h和和list.c中定义的AList类型及其操作函数
/*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;
}
}
/*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/C++ code
/*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 256ALIST *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));elsereturn(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));elsereturn(0);}int IoAddSortedItem(pList,pItem,compareTwoElements)ALIST *pList;void *pItem;#ifdef WIN32int (__cdecl *compareTwoElements)(const void **elem1,const void **elem2);#elseint (*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 WIN32int (__cdecl *compareTwoItems )(const void *item1,const void *item2);#elseint (*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;elsei=k+1;}}/*list.h */#ifndef _LIST_INCLUDED#define _LIST_INCLUDED#define ALIST_BLOCK_SIZE 100typedef struct {void **items;int numOfItems;} ALIST;/* --- The API functions for manipulating a list --- */#ifdef WIN32#ifdef __cplusplusextern "C" {#endifextern 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#elseextern 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
[解决办法]
我来帮顶!!!
弄点注释或许好点,什么都没有别人光看懂都不容易
[解决办法]
好的,顶
[解决办法]
接分
[解决办法]
顶
[解决办法]
差不多吧,直接照抄,把函数名字都改成Alist::
[解决办法]
好长啊~
帮顶了,楼主~