typedef char ElemType;
typedef struct
{
ElemType *buf;
unsigned int n;
unsigned int max;
}List;
List *CreateList(int);
void DestroyList(List *);
void ClearList(List *);
int ListAppend(List *,ElemType);
int ListInsert(List *,int,ElemType);
int ListDelete(List *,int);
int GetElement(List *,int,ElemType *);
int IsEmpty(List *);
int IsFull(List *);
List *CreateList(int i)
{
List *L;
L=(List *)malloc(sizeof(List));
if(!L)
return 0;
L->buf=(ElemType *)malloc(i*sizeof(ElemType));
if(!L->buf)
{
free(L);
return 0;
}
L->n=0;
L->max=i;
return L;
}
void DestroyList(List *L)
{
free(L->buf);
free(L);
}
void ClearList(List *L)
{
L->n=0;
}
int ListAppend(List *L,ElemType e)
{
if(IsFull(L))
return 0;
ListInsert(L,L->n+1,e);
/*L->n++;*/
return 1;
}
int ListInsert(List *L,int i,ElemType e)
{
ElemType *newb;
ElemType *p,*q;
if(i<1||i>L->n+1)
return 0;
if(L->n>=L->max)
{
newb=(ElemType *)realloc(L->buf,(L->max+10)*sizeof(ElemType));
if(!newb)
return 0;
L->buf=newb;
L->max+=10;
}
p=&L->buf[i-1];
for(q=&L->buf[L->n-1];q>=p;q--)
{
*(q+1)=*q;
}
*p=e;
L->n++;
return 1;
}
int ListDelete(List *L,int i)
{
ElemType *p,*q;
if(i<1||i>L->n)
return 0;
p=&L->buf[i-1];
/*q=L->buf+L->n-1;*/
q=&L->buf[L->n-1];
for(++p;p<=q;p++)
{
*(p-1)=*p;
}
L->n--;
return 1;
}
int GetElement(List *L,int i,ElemType *e)
{
ElemType *p;
if(i<1||i>L->n)
return 0;
p=&(L->buf)[i-1];
*e=*p;
return 1;
}
int IsEmpty(List *L)
{
if(L->n==0)
return 1;
else
return 0;
}
int IsFull(List *L)
{
if(L->n==L->max)
return 1;
else
return 0;
}
本程序通过tc2.0.