作者在 2010-05-09 19:57:20 发布以下内容
顺序表存储结构
#define MAXSIZE 100
#define Increment 10
typedef int DataType;
typedef struct
{
DataType *Elem;
int length;
int listsize;
}SeqList;
顺序表初始化
int InitList(SeqList &L)
{
L.Elem=(ElemType *)malloc(MAXSIZE*sizeof(DataType)) ;
if(!L.Elem)
{
printf("分配储存空间失败");
return(-1);
}
L.length=0;
L.ListSize=MAXSIZE;
return 1;
}
顺序表插入
int InsertList(SeqList &L,int i,DataType e)
{
int j;
if(i<1||i>L.length+1)
{printf("插入位置错误"); returned-1;}
if(L.length>=L.ListSize)
{
newbase=(DataType *)realloc(L.elem,(L.listsize+INCREMENT)*sizeof(DataType));
if(!newbase)
{
printf("分配储存空间失败");
returned (-1);
}
L.elem=newbase;
L.listsize+=INCREMENT;
}
for(j=L.length-1;j>=i-1;j--)
L.Elem[j+1]=L.Elem[j];
L.Elem[i-1]=e;
L.length++;
return 1;
}
顺序表删除
int deleteList(SeqList &L,int i,DataType &e)
{
if(i<1||i>L.length)
{printf("删除位置错误");returned-1;}
e=L.Elem[i-1];
for(j=i;j<=L.length-1;j++)
L.Elem[j-1]=L.Elem[j];
L.length--;
}
顺序表按值查找
int LocateElem(SeqList L,DataType e)
{
i=0;
while(i<L.length&&L.elem[i]!=e)
i++;
if(i>L.length) return -1;
return 1;
}
#define MAXSIZE 100
#define Increment 10
typedef int DataType;
typedef struct
{
DataType *Elem;
int length;
int listsize;
}SeqList;
顺序表初始化
int InitList(SeqList &L)
{
L.Elem=(ElemType *)malloc(MAXSIZE*sizeof(DataType)) ;
if(!L.Elem)
{
printf("分配储存空间失败");
return(-1);
}
L.length=0;
L.ListSize=MAXSIZE;
return 1;
}
顺序表插入
int InsertList(SeqList &L,int i,DataType e)
{
int j;
if(i<1||i>L.length+1)
{printf("插入位置错误"); returned-1;}
if(L.length>=L.ListSize)
{
newbase=(DataType *)realloc(L.elem,(L.listsize+INCREMENT)*sizeof(DataType));
if(!newbase)
{
printf("分配储存空间失败");
returned (-1);
}
L.elem=newbase;
L.listsize+=INCREMENT;
}
for(j=L.length-1;j>=i-1;j--)
L.Elem[j+1]=L.Elem[j];
L.Elem[i-1]=e;
L.length++;
return 1;
}
顺序表删除
int deleteList(SeqList &L,int i,DataType &e)
{
if(i<1||i>L.length)
{printf("删除位置错误");returned-1;}
e=L.Elem[i-1];
for(j=i;j<=L.length-1;j++)
L.Elem[j-1]=L.Elem[j];
L.length--;
}
顺序表按值查找
int LocateElem(SeqList L,DataType e)
{
i=0;
while(i<L.length&&L.elem[i]!=e)
i++;
if(i>L.length) return -1;
return 1;
}