(议论发分100!)用C++的类(class)重新实现在list.h和和list.c中定义的AList类型及其操作函数
(讨论发分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)
/*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)