今天晚上把顺序表的基本操作写了一下
#include<stdio.h>
#include<malloc.h>
#define ElemType int
#define Status int
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct SQ{
ElemType *Elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *L);
Status StructSqList(SqList *L);
Status InsertSqList(SqList *L,int i,ElemType e);
Status DeleteSqList(SqList *L,int i);
Status SearchSqList(SqList *L,ElemType e);
Status MergeSqList(SqList *L1,SqList *L2,SqList *L3);
int main(int argc,char** argv){
int i,p,v;
SqList *L=(SqList *)malloc(sizeof(SqList));
if(!InitList_Sq(L))
printf("fail");
StructSqList(L);
printf("insert into a place and the value:");
scanf("%d%d",&p,&v);
InsertSqList(L,p,v);
printf("delete a place:");
scanf("%d",&p);
DeleteSqList(L,p);
printf("Search the value in what place:");
scanf("%d",&v);
SearchSqList(L,v);
printf("\n\n\nlength:%d",L->length);
for(i=1;i<=L->length;i++)
printf("\nthe %d number is :%d",i,L->Elem[i-1]);
printf("\n");
SqList *L2=(SqList *)malloc(sizeof(SqList));
InitList_Sq(L2);
StructSqList(L2);
SqList *L3=(SqList *)malloc(sizeof(SqList));
InitList_Sq(L3);
MergeSqList(L,L2,L3);
printf("\n\n\nlength:%d",L3->length);
for(i=1;i<L3->length;i++)
printf("\nthe %d number is :%d",i,L3->Elem[i-1]);
return 0;
}
Status InitList_Sq(SqList *L){
L->Elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->Elem)
return 0;
L->length=0;
L->listsize=LIST_INIT_SIZE;
return 1;
}
Status StructSqList(SqList *L){
ElemType x=0;
printf("The length now is %d,input the %d :",L->length,L->length+1);
scanf("%d",&x);
while(x!=9999){
if(L->length>=L->listsize){
printf("can't add");
break;
}
L->Elem[L->length]=x;
L->length++;
printf("the length now is %d,input the %d :",L->length,L->length+1);
scanf("%d",&x);
}
return 1;
}
Status InsertSqList(SqList *L,int i,ElemType e){
if(i<0||i>L->length+1){
printf("insert into a wrong place\n");
return 0;
}
int j;
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;
}
Status DeleteSqList(SqList *L,int i){
if(i<0||i>L->length){
printf("delete a wrong place ");
return 0;
}
int j;
for(j=i-1;j<L->length-1;j++)
L->Elem[j]=L->Elem[j+1];
L->length--;
return 1;
}
Status SearchSqList(SqList *L,ElemType e){
int i,count=0;
for(i=0;i<L->length;i++){
if(L->Elem[i]==e){
printf("%d is in the place of %d\n",e,i+1);
count++;
continue;
}
}
if(count==0){
printf("we don't have a %d",e);
return 0;
}
return 1;
}
Status MergeSqList(SqList *L1,SqList *L2,SqList *L3){
int i=0,j=0,z=0;
while(i<L1->length&&j<L2->length){
if(L1->Elem[i]<L2->Elem[j]){
L3->Elem[z]=L1->Elem[i];z++;i++;
}else{
L3->Elem[z]=L2->Elem[j];z++;j++;
}
}
while(i<L1->length){
L3->Elem[z]=L1->Elem[i];z++;i++;
}
while(j<L2->length){
L3->Elem[z]=L2->Elem[j];z++;j++;
}
L3->length=z+1;
return 1;
}