数据结构纯C语言版——顺序表实现及有序表的定义

#include
#include
#include
#include
typedef int Elemtype;
typedef int Status;
#define MAXSIZE 20
#undef OVERFLOW
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
//顺序表数据类型定义——动态分配 
typedef struct{
	Elemtype *data;
	int length;
	int listSize;
}SqList; 
/*
//静态分配
typedef struct{
	Elemtype data[MAXSIZE];
	int length;
}SqList;
*/
Status InitSqList(SqList *Sq);//初始化
Status DestorySqList(SqList *Sq);//销毁
Status ClearSqList(SqList *Sq);//清除
Status SqListEmpty(SqList Sq);//判断是否为空
Status InsertElem(SqList *Sq,Elemtype e,int i);//在位置i上插入元素e 
Status DeleteElem_i(SqList *Sq,int i,Elemtype *e);//删除第i个元素并返回元素值 
int DeleteElem_e(SqList *Sq,Elemtype e);//删除第一个值位e的元素并返回删除的元素位置
int LocalElem(SqList Sq,Elemtype e);//查找第一个值为e的元素并返回元素位置
Status ExitElem(SqList *Sq,Elemtype e,int k);//将第i个位置的元素替换为e
//获取顺序表的长度
int GetLength(SqList Sq);
//获取第i个数据元素的值
Status GetElem(SqList Sq,int i,Elemtype *e);
//归并算法实现——有序表的合并
void MergeList_Sq(SqList *LA,SqList *LB,SqList *LC);
int main(){
	SqList La,Lb,Lc;
	int i;Elemtype e;
	InitSqList(&La);
	InsertElem(&La,1,1);
	InsertElem(&La,3,2);
	InsertElem(&La,5,3);
	InsertElem(&La,6,4);
	InsertElem(&La,9,5);
	for(i=0;i<La.length;i++){
		printf("%d ",La.data[i]);
	}
	printf("La.lenth=%d\n",GetLength(La));
	InitSqList(&Lb);
	InsertElem(&Lb,2,1);
	InsertElem(&Lb,3,2);
	InsertElem(&Lb,4,3);
	InsertElem(&Lb,6,4);
	InsertElem(&Lb,8,5);
	for(i=0;i<Lb.length;i++){
		printf("%d ",Lb.data[i]);
	}
	printf("Lb.lenth=%d\n",GetLength(Lb));
	InitSqList(&Lc);
	MergeList_Sq(&La,&Lb,&Lc);
	for(i=0;i<Lc.length;i++){
		printf("%d ",Lc.data[i]);
	}
	printf("Lc.lenth=%d\n",GetLength(Lc));
	return 0;
}
Status InitSqList(SqList *Sq){//初始化
	Sq->data=(Elemtype *)malloc(sizeof(Elemtype));
	if(!Sq->data){
		return OVERFLOW;
	} 
	Sq->length=0;
	Sq->listSize=MAXSIZE;
	return OK;
}
Status DestorySqList(SqList *Sq){//销毁——动态分配 
	if(Sq->data){
		free(Sq->data);
		Sq->data=NULL;
		Sq->length=0;
		Sq->listSize=0; 
	}
	return OK;
}
Status ClearSqList(SqList *Sq){
	//清除——没有删除数据但是有效长度置为0 
	Sq->length=0;
}
Status SqListEmpty(SqList Sq){
	//判断是否为空
	return (!Sq.length);
}
Status InsertElem(SqList *Sq,Elemtype e,int i){
	//在第i个位置上插入元素e
	if(i<=0||i>Sq->length+1){//
		return ERROR;
	}
	if(Sq->length==Sq->listSize){//
		return ERROR;//
	}//
	int j;
	for(j=Sq->length;j>=i;j--){
		Sq->data[j]=Sq->data[j-1];
	}
	Sq->data[i-1]=e;
	Sq->length++; //
	return OK; 
} 
Status DeleteElem_i(SqList *Sq,int i,Elemtype *e){
	//删除第i个元素并返回元素值 
	if(i<=0||i>Sq->length){
		return ERROR;
	}
	*e=Sq->data[i-1];
	int j;
	for(j=i-1;j<Sq->length;j++){
		Sq->data[j]=Sq->data[j+1];
	}
	Sq->length--;
	return OK;	
}
int DeleteElem_e(SqList *Sq,Elemtype e){//删除第一个值位e的元素并返回删除的元素逻辑位置
	//Elemtype data_e;
	int i,j;
	for(i=0;i<Sq->length;i++){
		if(e==Sq->data[i]){
			break;
		}
	} 
	for(j=i;j<Sq->length-1;j++){
		Sq->data[j]=Sq->data[j+1];
	}
	if(i==Sq->length){
		return -1;
	}
	Sq->length--; 
	return i;
}
int LocalElem(SqList Sq,Elemtype e){//查找第一个值为e的元素并返回元素位置
	int i;
	for(i=0;i<Sq.length;i++){
		if(e==Sq.data[i]){
			break;
		}
	}
	if(i==Sq.length){
		return -1;
	}
	return i;
}
Status ExitElem(SqList *Sq,Elemtype e,int k){//将第k个位置的元素替换为e
	if(k<=0||k>Sq->length){
		return ERROR;
	}
	Sq->data[k-1]=e;
	return OK;
}
int GetLength(SqList Sq){
	return Sq.length;
}
Status GetElem(SqList Sq,int i,Elemtype *e){
	if(i<0||i>=Sq.length){
//		printf("i=%d Length=%d\n",i,Sq.length);
		return ERROR;
	}
//	printf("***\n");
	*e=Sq.data[i];
	return OK;
}
void MergeList_Sq(SqList *La,SqList *Lb,SqList *Lc){//有序表的合并 ——由小到大 
	int i=0,j=0,k=1;
//	Lc->length= La->length+Lb->length;
//	Lc->data=(Elemtype *)malloc(sizeof(Elemtype)*(Lc->length));
	Elemtype ai,bj;
	while(i<La->length&&j<Lb->length){
		GetElem(*La,i,&ai);
		GetElem(*Lb,j,&bj);
		if(ai<=bj){
			InsertElem(Lc,ai,k++);
			i++;
		}else{
			InsertElem(Lc,bj,k++);
			j++;
		}
	}
	while(i<La->length){
		GetElem(*La,i++,&ai);
		InsertElem(Lc,ai,k++);
	}
	while(j<Lb->length){
		GetElem(*Lb,j++,&bj);
		InsertElem(Lc,bj,k++);
	}
}

你可能感兴趣的:(数据结构与算法,c语言,数据结构)