【理解】数据结构 顺序线性表

记录
数据结构 顺序线性表(按照课本的伪代码给的函数及其参数)

19.10.24 SqList By Myself

最后一点问题就是,销毁和清空,清空之后就和销毁差不多。。。
有一点成就感哈哈

#include<iostream>
#include<cstdlib>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int ElemType;
// Status 是函数的类型,其值是函数结果状态代码 
typedef int Status;

typedef struct {
	ElemType * elem;	//基址
	int length;
	int listsize;
}SqList;

Status InitList_Sq(SqList &L) {
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
	if(!L.elem) exit(OVERFLOW);		//内存不够	//异常结束语句 
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	//输入链表元素
	cout << "请输入链表元素(空格隔开,负数结束):" << endl;
	int num = 0;
	for(int i = 0;num >= 0;i++) {
		cin >> num;
		if(num >= 0) {
			L.elem[i] = num;
			L.length++;
		}
		else {
			cout << "负数,终止输入!" << endl;
			num = 0;
			break;
		}
		if(L.length == L.listsize) {
			cout << "空间不足!" << endl;
			L.elem = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));	//追加存储空间 
			L.listsize = L.listsize + LISTINCREMENT;
			cout << "空间已增加!请重新输入!" << endl;
			num = 0;
			i--;
			break;
		}
	}
	cout << "初始化完成!" << endl;
	return OK; 
}//InitList_Sq

Status DestoryList(SqList &L) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	cout << "开始销毁..." << endl;
	for(int i = 0;i < L.length;i++) {
		L.elem[i] = '\0';
		//free(L.elem[i]);	//试一下 果然不行。。。 
	}
	L.elem = NULL;
	L.length = 0;
	L.listsize = 0;
	free(L.elem);
	cout << "已销毁。" << endl;
}//DestoryList

Status ClearList(SqList &L) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	cout << "开始清空..." << endl;
	for(int i = 0;i < L.length;i++) {
		L.elem[i] = '\0';
	}
	L.elem = NULL;
	L.length = 0;
	cout << "已清空。" << endl;
}//ClearList

Status ListEmpty(SqList L) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	if(L.length != 0) {
		cout << "非空" << endl;
		return TRUE;
	}
	if(L.length == 0) {
		cout << "为空" << endl;
		return FALSE;
	}
	cout << "判断完毕。" << endl;
}//ListEmpty

Status ListLength(SqList L) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	int i = 0;
	for(i = 0;L.elem[i] != '\0';i++);
	L.length = i;
	cout << "顺序表长度为:" << L.length << endl;
}//ListLength

Status GetElem(SqList L,int i,ElemType *elem) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	if(i < 0 || i > L.length) {
		cout << "位置不存在!" << endl;
		return ERROR;
	}
	*elem = L.elem[i-1];
	cout << "获取元素成功!" << endl;
	return OK;
}//GetElem

Status LocateElem(SqList L,ElemType elem) {		//这个里面的compare()函数暂时做不到。。。 
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	int f = 0;
	for(int i = 0;i < L.length;i++) {
		if(L.elem[i] == elem) {
			cout << "元素位置为:" << i+1 << endl;
			f = 1;
			cout << "定位元素成功!" << endl;
			return i+1;
		}
	}
	if(f == 0) {
		cout << "线性表中不存在该元素!" << endl;
	}
	return OK;
}//LocateElem

Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	int f = 0;
	for(int i = 0;i < L.length;i++) {
		if(L.elem[i] == cur_e) {
			//cout << "位置为:" << i+1 << endl;
			if(i+1 == 1) {
				cout << "无前驱" << endl;
				return OK;
			}
			*pre_e = L.elem[i-1];
			f = 1;
			return i+1;
		}
	}
	if(f == 0) {
		cout << "线性表中不存在该元素!" << endl;
	}
	return OK;
}//PriorElem

Status NextElem(SqList L,ElemType cur_e,ElemType *next_e) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	int f = 0;
	for(int i = 0;i < L.length;i++) {
		if(L.elem[i] == cur_e) {
			//cout << "位置为:" << i+1 << endl;
			if(i+1 == L.length) {
				cout << "无后继" << endl;
				return OK;
			}
			*next_e = L.elem[i+1];
			f = 1;
			return i+1;
		}
	}
	if(f == 0) {
		cout << "线性表中不存在该元素!" << endl;
	}
	return OK;
}//NextElem

Status ListInsert(SqList &L,int i,ElemType e) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	if(i < 0 || i > L.length) {
		cout << "位置不存在!" << endl;
		return ERROR;
	}
	for(int j = L.length;j >= i;j--) {
		L.elem[j] = L.elem[j-1];
	}
	L.elem[i-1] = e;
	++L.length;		//这里的++放前放后一样吗
	cout << "插入完成!" << endl;
	return OK; 
}//ListInsert	//在第 i 个元素位置之前

Status ListDelete(SqList &L,int i,ElemType *e) {
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	if(i < 0 || i > L.length) {
		cout << "位置不存在!" << endl;
		return ERROR;
	}
	*e = L.elem[i-1];
	L.elem[i-1] = '\0';
	for(int j = i-1;j < L.length-1;j++) {
		L.elem[j] = L.elem[j+1];
	}
	L.elem[L.length] = '\0';
	--L.length;		//这里的--放前放后一样吗
	cout << "删除完成!" << endl;
	return *e;
}//ListDelete

Status ListTraverse(SqList L) {		//这个里面的visit()函数暂时做不到。。。 
	if(!L.elem) {
		cout << "线性表不存在!" << endl;
		return ERROR;
	}
	for(int i = 0;i < L.length;i++) {
		cout << L.elem[i] << " ";
	}
	cout << "遍历完毕!" << endl;
}//ListDelete

void Menu() {
	cout << "1.---初始化一个线性表" << endl;
	cout << "2.---销毁线性表" << endl;
	cout << "3.---清空线性表" << endl;
	cout << "4.---判断线性表是否为空" << endl;
	cout << "5.---求线性表长度" << endl;
	cout << "6.---获取线性表指定位置元素" << endl;
	cout << "7.---获取线性表指定元素位置" << endl;
	cout << "8.---求前驱" << endl;
	cout << "9.---求后继" << endl;
	cout << "10.---在线性表指定位置插入元素" << endl;
	cout << "11.--删除线性表指定位置元素" << endl;
	cout << "12.--遍历线性表" << endl;
	cout << "负数.--退出" << endl;
}

int main() {
	SqList L;
	int select = 1;
	Menu();
	while(select >= 0) {
		cout << "请选择:" << endl;
		cin >> select;
		if(select > 12) {
			cout << "输入错误,请重新输入" << endl;
			continue;
		}
		if(select < 0) {
			cout << "程序退出!" << endl;
		}
		switch(select) {
			case 1:
				InitList_Sq(L);
				break;
			case 2:
				DestoryList(L);
				break;
			case 3:
				ClearList(L);
				break;
			case 4:
				ListEmpty(L);
				break;
			case 5:
				ListLength(L);
				break;
			case 6:
				cout << "请输入位置:" << endl;
				int i6;
				ElemType elem6,*e6;	//元素 e 以及指向 e 的指针 
				e6 = &elem6;
				cin >> i6;
				GetElem(L,i6,e6);
				cout << "获取元素为:" << elem6 << endl;
				elem6 = '\0';
				break;
			case 7:
				cout << "请输入元素:" << endl;
				ElemType elem7;
				cin >> elem7;
				LocateElem(L,elem7);
				break;
			case 8:
				cout << "请输入当前元素:" << endl;
				ElemType cur_e8;
				cin >> cur_e8;
				ElemType pre,*pre_e;
				pre_e = &pre;
				PriorElem(L,cur_e8,pre_e);
				cout << "当前元素前驱为:" << pre << endl;
				pre = '\0';
				break;
			case 9:
				cout << "请输入当前元素:" << endl;
				ElemType cur_e9;
				cin >> cur_e9;
				ElemType next,*next_e;
				next_e = &next;
				NextElem(L,cur_e9,next_e);
				cout << "当前元素后继为:" << next << endl;
				next = '\0';
				break;
			case 10:
				int i10;
				ElemType elem10;
				cout << "请输入位置:" << endl;
				cin >> i10;
				cout << "请输入元素:" << endl;
				cin >> elem10;
				ListInsert(L,i10,elem10);
				break;
			case 11:
				int i11;
				cout << "请输入位置:" << endl;
				cin >> i11;
				ElemType elem11,*e11;
				e11 = &elem11;
				ListDelete(L,i11,e11);
				cout << "被删除的元素为:" << elem11 << endl;
				elem11 = '\0';
				break;
			case 12:
				ListTraverse(L);
				break;
		}
	}
	return 0;
}

这个获取元素或位置的输出这个读懂了自然就知道了:
https://blog.csdn.net/qq_43763494/article/details/101640686----【理解】指针


运行结果:
在这里插入图片描述
在这里插入图片描述

19.10.7 正解在此,先记下。。。

#include<iostream>
#include<cstdlib>
using namespace std;

typedef int ElemType;
typedef int Status;

#define List_Init_Size 100	//线性表存储空间的初始分配量
#define ListIncrement  10	//线性表存储空间的分配增量
#define OVERFLOW 0
#define Error 0
#define OK 1
#define True 1

typedef struct {
	ElemType * elem;	//存储空间基址	//说实话,没搞懂 
	int length;			//当前长度
	int listsize; 		//当前分配的存储容量(以 sizeof(ElemType)为单位)
}SqList;

//基本操作
//1.---初始化一个线性表
Status InitList_Sq(SqList &L) {
	//构造一个空的线性表L
	L.elem = (ElemType *)malloc(List_Init_Size * sizeof(ElemType));
	if(!L.elem) exit(OVERFLOW);		//存储分配失败
	L.length = 0;			//空表长度为0
	L.listsize = List_Init_Size;	//初始存储容量
	cout << "请输入顺序表内容(以负数为结束):" << endl;
	int num=0;
	for(int i=0;num>=0;i++){
		cin >> num;
		if(num>=0) {
			L.elem[i] = num;
			L.length++;
		}
		else {
			cout << "负数!输入终止" << endl;
			break;
		}
		if(L.length == L.listsize){
			cout << "空间不足!" << endl;
			break;
		}
	}
	return OK;
}//InitList_Sq

//初始化一个空表
Status InitList(SqList &L) {
	//构造一个空的线性表L
	L.elem = (ElemType *)malloc(List_Init_Size * sizeof(ElemType));
	if(!L.elem) exit(OVERFLOW);		//存储分配失败
	L.length = 0;			//空表长度为0
	L.listsize = List_Init_Size;	//初始存储容量
	return OK;
}//InitList

//11.--显示线性表
int CoutList(SqList &L)	//打印
{
	if(!L.elem) cout << "未找到顺序表!" << endl;
	else if(L.length == 0) cout << "顺序表为空!" << endl;
	else {
		cout << "顺序表内容为:" << endl;
		for(int i=0;i<L.length;i++){		//为什么有时候会输出一个很大的类似地址的数?现在没了 
			cout << L.elem[i] << " ";	//L.elem是地址
		}
	}
	cout << endl;
	return OK;
}

//2.---销毁线性表
int DestoryList(SqList &L)		//销毁
{
	cout << "*** 正在销毁... ***" << endl;
	for(int i=0;i<L.length;i++){
		L.elem = NULL;		//这个只能销毁地址吗 
	}
	L.length = 0;
	L.listsize = 0;
	free(L.elem);
	cout << "*** 已销毁. ***" << endl;
	return True;
}

//3.---清空线性表
int ClearList(SqList &L) 	//清空
{
	cout << "*** 正在清空... ***" << endl;
	for(int i=0;i<L.length;i++){
		L.elem[i] = '\0';
	}
	L.length = 0;
	cout << "*** 已清空. ***" << endl;
	return True;
}

//4.---判断线性表是否为空
int ListEmpty(SqList &L) 	//判空
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else if(L.length == 0) {
		cout << "顺序表为空!" << endl;
		return OK;
	}
	else {
		cout << "不为空!" << endl;
	}
	return OK; 
}

//5.---求线性表长度
int ListLength(SqList &L)	//表长
{
	cout << "表长为:" << L.length << endl;
	return OK;	
}

//6.---获取线性表指定位置元素
ElemType GetElem(SqList &L,int i)		//获取第i个元素值
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else if(i<=0 || i>L.length){
		cout << "未找到该位置!" << endl;
	}
	else {
		cout << "位于" << i << "位置的元素是:" << L.elem[i-1] << endl;  
	}
	return L.elem[i-1];
}

//int LocateElem(L,e,compare());	//定位

//7.---求前驱
int PriorElem(SqList &L,ElemType cur_e)		//返回前驱
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else {
		int flag = 0;
		for(int i=0;i<L.length;i++){
			if(cur_e == L.elem[i]){
				flag = 1;
				if(i == 0) cout << "无直接前驱!" << endl;
				else cout << "前驱:" << L.elem[i-1] << endl;
				break;
			}
		}
		if(flag == 0){
			cout << "未找到该元素!" << endl; 
		}
	}
	return OK;
}

//8.---求后继
int NextElem(SqList &L,ElemType cur_e)	//返回后继
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else {
		int flag = 0;
		for(int i=0;i<L.length;i++){
			if(cur_e == L.elem[i]){
				flag = 1;
				if(i+1 == L.length) cout << "无直接后继!" << endl;
				else cout << "后继:" << L.elem[i+1] << endl;
				break;
			}
		}
		if(flag == 0){
			cout << "未找到该元素!" << endl;
		}
	}
	return OK;
}

//9.---在线性表指定位置插入元素
int ListInsert(SqList &L,int i,ElemType e)	//插入
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else if(i<0 || i>L.length){
		cout << "未找到该位置!" << endl;
	}
	else {
		int oldL = L.length;
		ElemType t;
		for(int j=oldL;j>=i;j--){
			t = L.elem[j-1];
			L.elem[j] = t;
		}
		L.elem[i-1] = e;
		L.length++;
	}
	return OK;
}

//9.Lc---在线性表指定位置插入元素
int ListInsertLc(SqList &L,int i,ElemType e)	//插入
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else if(i<0 || i>L.length){
		cout << "未找到该位置!" << endl;
	}
	else {
		L.elem[i-1] = e;
	}
	return OK;
}

//10.--删除线性表指定位置元素
int ListDelete(SqList &L,int i)		//删除
{
	if(!L.elem) {
		cout << "未找到顺序表!" << endl;
		return Error;
	}
	else if(i<0 || i>L.length){
		cout << "未找到该位置!" << endl;
	}
	else {
		L.elem[i-1] = '\0';
		ElemType t;
		for(int j=i;j<L.length;j++){
			t = L.elem[j];
			L.elem[j-1] = t;
		}
		L.length--;
	}
	return OK;
}

/*void ListTraverse(L,visit());	//遍历 */

//12.--合并两个非递减顺序表
int MergeList(SqList La,SqList Lb,SqList &Lc)	//归并 
{
	//已知线性表La和Lb中的数据元素按值非递减排列
	//归并La和Lb得到新的线性表Lc,lc的数据元素也按值非递减排列
	InitList(Lc);
	int i=1,j=1,k=0; 
	int La_len = La.length;
	int Lb_len = Lb.length;
	Lc.length = La_len+Lb_len;
	while((i <= La_len) && (j <= Lb_len)){	//La和Lb均非空 
		ElemType ai = GetElem(La,i);
		ElemType bj = GetElem(Lb,j);
		if(ai <= bj) {
			ListInsertLc(Lc,++k,ai);
			++i;
		}
		else {
			ListInsertLc(Lc,++k,bj);
			++j;
		}
		CoutList(Lc);
	}
	while(i < La_len+1){	//这个真得加一,因为一个是从1开始,而一个是地址 
		ElemType ai = GetElem(La,i);
		++i;
		ListInsertLc(Lc,++k,ai);
	}
	while(j < Lb_len+1){
		ElemType bj = GetElem(Lb,j);
		++j;
		ListInsertLc(Lc,++k,bj);
	}
}//MergeList

void Menu()
{
	cout << "1.---初始化一个线性表" << endl;
	cout << "2.---销毁线性表" << endl;
	cout << "3.---清空线性表" << endl;
	cout << "4.---判断线性表是否为空" << endl;
	cout << "5.---求线性表长度" << endl;
	cout << "6.---获取线性表指定位置元素" << endl;
	cout << "7.---求前驱" << endl;
	cout << "8.---求后继" << endl;
	cout << "9.---在线性表指定位置插入元素" << endl;
	cout << "10.--删除线性表指定位置元素" << endl;
	cout << "11.--显示线性表" << endl;
	cout << "12.--合并两个非递减顺序表" << endl;
	cout << "负数.--退出" << endl;
}

int main()
{
	int select = 1;
		Menu();		//菜单单独放在一个函数里,显得简洁
	do{
		SqList L;
		cout << "请输入操作代码:" << " ";
		cin >> select;
		cout << endl;
		if(select < 0){
			cout << "正在退出.." << endl;
			break;
		}
		else if(select > 12){
			cout << "输入错误!" << endl;
			continue;
		}
		switch(select)
		{
			case 1:
				cout << "*** 顺序表初始化... ***" << endl;
				InitList_Sq(L);
				cout << "*** 初始化完成 ***" << endl;
				CoutList(L);
				break;
			case 2:
				cout << "销毁顺序表" << endl;
				DestoryList(L);
				CoutList(L);
				break;
			case 3:
				cout << "清空顺序表" << endl;
				ClearList(L);
				CoutList(L);
				break;
			case 4:
				cout << "判空" << endl;
				ListEmpty(L);
				break;
			case 5:
				cout << "判断顺序表长度" << endl;
				ListLength(L);
				break;
			case 6:
				cout << "请输入位置(get elem):" << endl;
				int i;
				cin >> i; 
				GetElem(L,i);
				break;
			case 7:
				cout << "请输入元素(get last):" << endl;
				ElemType cur_e;
				cin >> cur_e; 
				PriorElem(L,cur_e);
				break;
			case 8:
				cout << "请输入元素(get next):" << endl;
				ElemType e;
				cin >> e;
				NextElem(L,e);
				break;
			case 9:
				cout << "请输入位置和元素(insert):" << endl;
				ElemType enine;		//我也不知道为什么会重复名明
				int inine;
				cin >> inine >> enine;
				ListInsert(L,inine,enine);
				break;
			case 10:
				cout << "请输入位置(delete):" << endl;
				int iten;
				cin >> iten; 
				ListDelete(L,iten);
				break;
			case 11:
				CoutList(L);
				break;
			case 12:
				SqList La,Lb,Lc;
				cout << "*** 非递减顺序表L1初始化... ***" << endl;
				InitList_Sq(La);
				cout << "*** 初始化完成 ***" << endl;
				CoutList(La);
				cout << "*** 非递减顺序表L2初始化... ***" << endl;
				InitList_Sq(Lb);
				cout << "*** 初始化完成 ***" << endl;
				CoutList(Lb);
				cout << "*** 正在归并La和Lb... ***" << endl;
				MergeList(La,Lb,Lc);
				cout << "*** 归并完成 ***" << endl;
				cout << "归并结果:" << endl;
				CoutList(Lc);
				break;
		}
		/*cout << "正数.--返回主菜单" << endl;
		cout << "负数.--退出" << endl;
		cout << "请输入:" << " ";
		cin >> select;
		if(select < 0) cout << "正在退出.." << endl;*/
		cout << endl;
	}while(select >= 0);
	return 0;
}

功能实现:
在这里插入图片描述在这里插入图片描述
还有最后一个归并功能没实现
在这里插入图片描述
在这里插入图片描述
终于理解了一点线性表、结构体、链表、指针之类的东西,先记一下
关于指针是几句话说不完的:【理解】指针

19.10.5的理解,有点跑偏,这个就放到下面了

#include<iostream>
#include<cstdlib>
using namespace std;

/*写在前面:
在经历了这么多的探索与学习之后,终于搞懂了一些顺序表的原理所在,我觉得最重要的就是对于指针的总结与理解,有很多东西真正理解了才知道为什么
现在算是知道了,指针就是一个变量,只不过比较特殊而已,还是一个变量,变量就是一块空间的名称,只是一个名字而已
结构体类型的指针就是分配一个结构体相应大小的一块空间,这个空间的名字就是指针,可以用来存地址,还是不明白为什么要给一个指针这么大的空间,
不是只存了一个地址么?还是没有彻底理解...
关于指针是几句话说不完的:[【理解】指针](https://blog.csdn.net/qq_43763494/article/details/101640686)
存入顺序表的首地址以后,指针就可以代表顺序表了
仍然有好多不足之处难以理解,还需要继续理解与优化
满满的BUG让我。。。无言以对*/

struct SqList{
	int a[1024];	//顺序表的话,就是一个不含指针域的结构体
	int size;		//定义两个变量,一个用来存数据元素,一个用来存长度
};

//基本操作
SqList * InitList(SqList *L)	//初始化空表	//查了一下关于->和.的区别,都是"的"的意思,但是->需要一个指针
{
	L = (SqList *)malloc(sizeof(SqList));	//分配空间相关内容明天查查...在内存的动态存储区中分配一个长度为size的连续空间
	cout << "请输入顺序表内容(以负数为结束):" << endl;
	int num=0;
	for(int i=0;num>=0;i++){
		cin >> num;
		if(num>=0) L->a[i] = num;
		else {
			cout << "负数!输入终止" << endl;
			break;
		}
	}
	return L;	//要返回顺序表(结构体)首地址,就需要同样的函数类型
}

void CoutList(SqList *L)	//打印	//传递的参数是结构体类型的指针变量L,这个指针变量里存的的是顺序表的首地址
{
	cout << "顺序表内容为:" << endl;
	for(int i=0;L->a[i]!='\0';i++){		//为什么有时候会输出一个很大的类似地址的数? 
		cout << L->a[i] << " ";
	}
	cout << endl;
}

void DestoryList(SqList *L)		//销毁
{
	cout << "*** 正在销毁... ***" << endl;
	free(L);
	for(int i=0;L->a[i]!='\0';i++){
		L->a[i] = '\0';
	}
	cout << "*** 已销毁. ***" << endl;
} 

void ClearList(SqList *L) 	//清空
{
	cout << "*** 正在清空... ***" << endl;
	for(int i=0;L->a[i]!='\0';i++){
		L->a[i] = '\0';
	}
	cout << "*** 已清空. ***" << endl;
}

/*void ListEmpty(&L);	//判空

int ListLength(&L);	//表长

void GetElem(L,i,&e);	//获取第i个元素值

void LocateElem(L,e,compare());	//定位

void PriorElem(L,cur_e,&pre_e);	//返回前驱

void NextElem(L,cur_e,&next_e);	//返回后继

void ListInsert(&L,i,e);	//插入

void ListDelete(&L,i,&e);	//删除

void ListTraverse(L,visit());	//遍历
*/

void Menu()
{
	cout << "1.---初始化一个线性表" << endl;
	cout << "2.---销毁线性表" << endl;
	cout << "3.---清空线性表" << endl;
	cout << "4.---判断线性表是否为空" << endl;
	cout << "5.---求线性表长度" << endl;
	cout << "6.---获取线性表指定位置元素" << endl;
	cout << "7.---求前驱" << endl;
	cout << "8.---求后继" << endl;
	cout << "9.---在线性表指定位置插入元素" << endl;
	cout << "10.--删除线性表指定位置元素" << endl;
	cout << "11.--显示线性表" << endl;
	cout << "12.--合并两个非递减顺序表" << endl;
	cout << "负数.--退出" << endl;
}

int main()
{
	int select = 1;
	do{
		SqList *L;		//这只是定义一个SqList结构体类型的指针,里面要存一个结构体的首地址 
		//int a[1024];
		//L = a;	//SqList类型中已经包含了存数据元素的数组了
		Menu();		//菜单单独放在一个函数里,显得简洁
		cout << "请输入操作代码:" << " ";
		cin >> select;
		cout << endl;
		if(select < 0){
			cout << "正在退出.." << endl;
			break;
		}
		switch(select)
		{
			case 1:
				cout << "*** 顺序表初始化... ***" << endl;
				L = InitList(L);	//在指针L里面放入因调用函数而返回的顺序表首地址
				cout << "*** 初始化完成 ***" << endl;
				CoutList(L);
				break;
			case 2:
				cout << "销毁顺序表" << endl;
				DestoryList(L);
				CoutList(L);
				break;
			case 3:
				cout << "清空顺序表" << endl;
				ClearList(L);
				CoutList(L);
				break;
			case 11:
				if(L == NULL) cout << "未找到顺序表!" << endl;
				else if(L->a[0] == '\0') cout << "顺序表为空!" << endl;
				else CoutList(L);
				break;
		}
		/*cout << "1.-----返回主菜单" << endl;
		cout << "负数.--退出" << endl;
		cout << "请输入:" << " "; 
		cin >> select;*/
		cout << endl;
	}while(select >= 0);
	return 0;
}

哈哈,真服了我自己了



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值