顺序表的基本操作

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值