数据结构(C语言版)-顺序表的实现

注:为了方便编写,本人使用C++实现,与C语言并无太大差别,仅供参考;
完整代码:

#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 100
#define STEPSIZE 10
#define SUCCESS 1
#define ERROR -1
typedef int ElemType;
int flag=0;//用于判断顺序表是否初始化,默认为没有
typedef struct SqList{
	ElemType* list;
	int length;
}SqList;
//1-初始化顺序表
void InitList(SqList* L){
	if(flag==0){
		L->list=(ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
		L->length=0;
		if(!L->list){
			exit(ERROR);
		}
		flag=1;
		cout<<"顺序表初始化完毕!"<<endl;
	}else{
		cout<<"顺序表已经初始化!"<<endl;
	}
}
//2-销毁顺序表
void DestoryList(SqList* L){
	if(flag==1){
		free(L->list);
		flag=0;
		cout<<"顺序表已销毁!"<<endl;
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
	
}
//3-清空顺序表
void ClearList(SqList* L){
	if(flag==1){
		L->length=0;
		cout<<"顺序表已经被清空!"<<endl;
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
	
}
//4-判断顺序表是否为空,为空返回true
bool ListEmpty(SqList* L){
	if(flag==1){
		if(L->length==0){
			cout<<"顺序表为空!"<<endl;
			return true;
		}else{
			cout<<"顺序表不为空!"<<endl;
			return false;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
		return false;
	}
}
//5-返回顺序表的长度
int ListLength(SqList* L){
	if(flag==1){
		return L->length;
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
		return ERROR;
	}
}
//6-用e返回L中第i个元素值
ElemType GetElem(SqList* L,int i,ElemType &e){
	if(flag==1){
		if(i>0&&i<=L->length){
			e=L->list[i-1];
			return e;
		}else{
			cout<<"位置输入不合法!"<<endl;
			return NULL;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
}
//7-返回元素e的位置
int LocateElem(SqList* L,int &i,ElemType e){
	if(flag==1){
		for(i=0;i<L->length;i++){
			if(L->list[i]==e){
				i=i+1;
				return i;
			}
		}
		return ERROR;
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
		return ERROR;
	}
}
//8-返回元素e的直接前驱
ElemType PriorElem(SqList* L,ElemType cur_e,ElemType &pre_e){
	int i;
	if(flag==1){
		if(LocateElem(L,i,cur_e)>0){
			if(LocateElem(L,i,cur_e)!=1){
				pre_e=L->list[i-2];
				cout<<"元素 "<<cur_e<<" 的直接前驱是:"<<pre_e<<endl;
				return pre_e;
			}else{
				cout<<"元素 "<<cur_e<<" 是第一个元素,不存在直接前驱!"<<endl;
			}
		}else{
			cout<<"元素 "<<cur_e<<" 不存在顺序表中!"<<endl;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
}
//9-返回元素e的直接后继
ElemType NextElem(SqList* L,ElemType cur_e,ElemType &next_e){
	int i;
	if(flag==1){
		if(LocateElem(L,i,cur_e)>0){
			if(LocateElem(L,i,cur_e)!=ListLength(L)){
				next_e=L->list[i];
				cout<<"元素 "<<cur_e<<"的直接后继是:"<<next_e<<endl;
				return next_e;
			}else{
				cout<<"元素 "<<cur_e<<" 是最后一个元素,不存在直接后继!"<<endl;
				return ERROR;
			}
		}else{
			cout<<"元素 "<<cur_e<<" 不存在顺序表中!"<<endl;
			return ERROR;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
		return ERROR;
	}
}
//10-在第i个位置插入数据e
void ListInsert(SqList* L,int i,ElemType e){
	int j;
	if(flag==1){
		if(L->length>=MAXSIZE){
			L->list=(ElemType*)realloc(L->list,sizeof(ElemType)*STEPSIZE);
		}
		if(i>0 && i<=L->length+1){
			for(j=L->length;j>=i;j--){
				L->list[j]=L->list[j-1];
			}
			L->list[i-1]=e;
			L->length++;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
}
//11-删除第i个元素
void ListDelete(SqList* L,int i,ElemType &e){
	int j;
	if(flag==1){
		if(i>0 && i<=ListLength(L)){
			e=L->list[i-1];
			for(j=i;j<ListLength(L);j++){
				L->list[j-1]=L->list[j];
			}
			L->length--;
			cout<<"删除成功!";
		}else{
			cout<<"输入位置不正确!"<<endl;
		}
	}else{
		cout<<"顺序表不存在!无法操作!"<<endl;
	}
}

//12-显示顺序表
void ListShow(SqList* L){
	int i;
	cout<<"打印长度为"<<L->length<<"的顺序表:";
	for(i=0;i<L->length;i++){
		cout<<L->list[i]<<"-";
	}
	cout<<endl;
}
//主菜单
void menu(SqList* L){
	int i,n,pre_e,cur_e,next_e;
	ElemType e;
	cout<<"***********************************************"<<endl;
	cout<<"**************1、初始化顺序表******************"<<endl;
	cout<<"**************2、销毁顺序表********************"<<endl;
	cout<<"**************3、清空顺序表********************"<<endl;
	cout<<"**************4、判断顺序表是否为空************"<<endl;
	cout<<"**************5、返回顺序表的长度**************"<<endl;
	cout<<"**************6、返回第i个元素的值*************"<<endl;
	cout<<"**************7、返回元素e的位置***************"<<endl;
	cout<<"**************8、返回元素e的直接前驱***********"<<endl;
	cout<<"**************9、返回元素e的直接后继***********"<<endl;
	cout<<"**************10、在第i个位置插入元素e*********"<<endl;
	cout<<"**************11、删除第i个元素****************"<<endl;
	cout<<"**************12、遍历顺序表*******************"<<endl;
	cout<<"**************13、退出*************************"<<endl;
	cout<<"***********************************************"<<endl;
	cout<<"请选择:";
	cin>>n;
	switch(n){
	case 1:
		InitList(L);
		menu(L);
	case 2:
		DestoryList(L);
		menu(L);
	case 3:
		ClearList(L);
		menu(L);
	case 4:
		ListEmpty(L);
		menu(L);
	case 5:
		cout<<"当前顺序表长度:"<<ListLength(L)<<endl;
		menu(L);
	case 6:
		cout<<"请输入位置";
		cin>>i;
		GetElem(L,i,e);
		cout<<"位置"<<i<<"上的元素是:"<<e<<endl;
		menu(L);
	case 7:
		cout<<"输入数据e:";
		cin>>e;
		LocateElem(L,i,e);
		if(LocateElem(L,i,e)>0){
			cout<<"元素"<<e<<"的位置是:"<<i<<endl;
		}else{
			cout<<"未查询到!"<<endl;
		}
		menu(L);
	case 8:
		cout<<"请输入元素e:";
		cin>>cur_e;
		PriorElem(L,cur_e,pre_e);
		menu(L);
	case 9:
		cout<<"请输入元素e:";
		cin>>cur_e;
		NextElem(L,cur_e,next_e);
		menu(L);
	case 10:
		cout<<"请输入数据e:";
		cin>>e;
		cout<<"请输入插入位置:";
		cin>>i;
		ListInsert(L,i,e);
		menu(L);
	case 11:
		cout<<"请输入要删除的位置i:";
		cin>>i;
		ListDelete(L,i,e);
		cout<<"删除的元素是 "<<e<<endl;
		menu(L);
	case 12:
		ListShow(L);
		menu(L);
	}
}
void main(){
	SqList L;
	menu(&L);
	/*InitList(&L);
	DestoryList(&L);
	ListInsert(&L,1,1);
	ListInsert(&L,2,2);
	ListInsert(&L,3,3);
	ListInsert(&L,4,4);
	ListInsert(&L,5,5);
	ListShow(&L);
	ListInsert(&L,6,9);
	ListShow(&L);
	ClearList(&L);
	ListShow(&L);
	*/
	system("pause");
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值