顺序表实现

今天晚上把顺序表的基本操作写了一下

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值