顺序表源代码
#include <stdio.h>
#include <stdlib.h>
#define list_init_size 100//线性表存储空间的初始分配量
#define listincresement 100//线性表存储空间额分配增量
#define OK 1
#define ERROR 0
typedef int elemType;
typedef struct
{
elemType *List;
int length;
int listsize;
}Sqlist;
void Againmalloc(Sqlist *L)
{
elemType *newbase;
newbase=(elemType *)realloc(L->List,(L->listsize+listincresement)*sizeof(elemType));
if(!newbase)
exit(-1);
L->List=newbase;
L->listsize+=listincresement;
}
void initlistsq(Sqlist *L)
{
L->List=(elemType *)malloc(list_init_size*sizeof(elemType));
if(!L->List)
exit(-1);
L->length=0;
L->listsize=listincresement;
}
int listlength(Sqlist *L)
{
return L->length;
}
void traverselist(Sqlist *L)
{
int i;
for(i=0;i<L->length;i++)
{
printf("%d ",L->List[i]);
}
printf("\n");
}
void insertfirst(Sqlist *L,elemType e)//表头插入元素
{
int i;
if(L->length>=L->listsize)
Againmalloc(L);
for(i=L->length-1;i>=0;i--)
L->List[i+1]=L->List[i];
L->List[0]=e;
L->length++;
}
void insertlast(Sqlist *L,elemType e)
{
if(L->length>=L->listsize)
Againmalloc(L);
L->List[L->length]=e;
L->length++;
}
void insert(Sqlist *L,elemType e,int pos)//第pos处插入元素
{
int i;
if(pos<1||pos>L->length+1)
exit(-1);
if(L->length>=L->length+1)
Againmalloc(L);
for(i=L->length-1;i>=pos-1;i--)
L->List[i+1]=L->List[i];
L->List[pos-1]=e;
L->length++;
}
void search(Sqlist *L,elemType e)
{
int i;
for(i=0;i<L->length;i++)
{
if(L->List[i]==e)
{
printf("已找到,%d在第%d个位置\n",e,i+1);
return ;
}
}
printf("没找到\n");
return ;
}
elemType deleteelem(Sqlist *L,int pos)
{
int i;
elemType temp;
if(pos<1||pos>L->length)
{
printf("越界\n");
exit(-1);
}
temp=L->List[pos-1];
for(i=pos;i<L->length;i++)
L->List[i-1]=L->List[i];
L->length--;
return temp;
}
int isempty(Sqlist *L)
{
if(L->length==0)
return 1;
else
return 0;
}
void inversation(Sqlist *L)
{
elemType temp;
int i;
for(i=0;i<L->length/2;i++)
{
temp=L->List[i-1];
L->List[i-1]=L->List[L->length-i];
L->List[L->length-i]=temp;
}
}
void mergelist(Sqlist *La,Sqlist *Lb,Sqlist *Lc)
{
Lc->listsize=Lc->length=La->length+Lb->length;
Lc->List=(elemType *)malloc(sizeof(elemType));
if(!Lc->List)
exit(-1);
int i=0,j=0,k=0;
while(i<La->length&&j<Lb->length)
{
if(La->List[i]<=Lb->List[j])
{
Lc->List[k++]=La->List[i++];
}
else
{
Lc->List[k++]=Lb->List[j++];
}
}
while(i<La->length)
{
Lc->List[k++]=La->List[i++];
}
while(j<Lb->length)
{
Lc->List[k++]=Lb->List[j++];
}
}
void sort(Sqlist *L)
{
int i,j,k,temp;
for(i=1;i<L->length;i++)
{
temp=L->List[i];
for(j=i-1;j>=0&&L->List[j]>temp;j--)
{
L->List[j+1]=L->List[j];
}
L->List[j+1]=temp;
}
}
int main()
{
Sqlist list1;
initlistsq(&list1);
int length;
scanf("%d",&length);
int i;
elemType temp;
for(i=0;i<length;i++)
{
scanf("%d",&temp);
insertlast(&list1,temp);
}
printf("创建好的线性表La=\n");
traverselist(&list1);
int pos;
scanf("%d%d",&temp,&pos);
insert(&list1,temp,pos);
printf("插入一个元素后的线性表La=\n");
traverselist(&list1);
scanf("%d",&temp);
search(&list1,temp);
printf("倒置后的线性表La=\n");
inversation(&list1);
traverselist(&list1);
Sqlist list2;
initlistsq(&list2);
scanf("%d",&length);
for(i=0;i<length;i++)
{
scanf("%d",&temp);
insertlast(&list2,temp);
}
Sqlist list3;
mergelist(&list1,&list2,&list3);
printf("合并后的线性表=\n");
traverselist(&list3);
printf("排序后的线性表\n");
sort(&list3);
traverselist(&list3);
return 0;
}