数据结构--线性
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
的顺序存储表示附源程序(转)
typedef int ElemType;
//元素的数据类型 typedef long Status;
//函数的返回数据类型
typedef struct
{
ElemType
*elem;
//存储空间基址—动态分配的一维数组
int length;
int listsize;
}List;
#define MAXSIZE 10000
#define SIZEINCREASE
100
#define TRUE
1
#define FALSE
0
#define OK
1
#define ERROR
0
#define INFEASIBLE
-1
#define OVERFLOW
-2
Status initlist(List *L)
{
L->elem=(ElemType *)malloc(MAXSIZE*sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);//存储分配失败
L->length=0;
//空表长度为0
L->listsize=MAXSIZE;
//初始化存储容量
return OK;
}
Status insert(List *L)
{
ElemType x;
int i,k;
x=rand()%100;
i=rand()%50;
/*printf("Please enter the Element:
");
scanf("%2d",&x);
printf("Please enter the Location:
");
scanf("%2d",&i);*/
if(i<1||i>L->length+1)
L->elem[L->length]=x;
if(L->length==0&&i==1)
{
L->elem[0]=x;
L->length++;
}
else
{
for(k=L->length;k>=i;k--)
{
L->elem[k+1]=L->elem[k];
}
L->elem[i-1]=x;
L->length++;
}
return OK;
}
Status delelem(List *L)
{
int i;
ElemType e;
printf("the location you want to delete:
");
scanf("%d",&i);
printf("the element you delete is: %d\n",L->elem[i-1]);
if(i<1||i>L->length)
{
printf("ERROR\n");
return ERROR;
}
else
{
ElemType *p,*q;
p=&(L->elem[i-1]);
e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;p++)
{
*(p-1)=*p;
}
}
L->length--;
return e;
}
int Partition(List *L,int low,int high)
{
ElemType key;
key=L->elem[low];
while(low
elem[high]>=key))
--high;
L->elem[low]=L->elem[high];
while((lowelem[low]<=key))
++low;
L->elem[high]=L->elem[low];
}
L->elem[low]=key;
return low;
}
void Qsort(List *L,int low,int high)
{
int result;
if(lowlength&&L->elem[ i]!=e)
i++;
if(ilength)
return i;
else return FALSE;
}
void addelem(List *L,ElemType e)
{
if(L->length==0)
{
L->elem[0]=e;
++L->length;
}else
{
++(L->length);
L->elem[L->length-1]=e;
}
}
void unionlist(List *La,List Lb)
{
int i;
for(i=1;i<=Lb.length;i++)
{
ElemType e;
e=readelem(Lb,i);
if(!comparelem(La,e)) addelem(La,e);
}
}
void mergethirdlist(List La,List Lb,List *L)
{
int i,j,k;ElemType ai,bj;
i=j=k=1;
while((i<=La.length)&&(j<=Lb.length))
{
ai=La.elem[i-1];
bj=Lb.elem[j-1];
if(ai<=bj)
{
L->elem[k-1]=ai;
k++;
i++;
}
else
{
L->elem[k-1]=bj;
k++;
j++;
}
}
while(i<=La.length)
{
L->elem[k-1]=ai;
k++;
i++;
}
while(j<=Lb.length)
{
L->elem[k-1]=bj;
k++;
j++;
}
L->length=k-1;//当i增加到List La的最大长度值的时候,还要再自增一次进行判断i是否
<=La.length
}