顺序表的基本操作

实验要求:
  • 编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
  • 采用顺序表结构编程实现:两个集合的运算:交集/并集/差集。
实验目的:

通过该实验,深入理解顺序表的逻辑结构、物理结构等概念,掌握顺序表基本操作的编程实现,注意顺序表插入、删除等操作过程中数据元素的移动现象,培养学生编写程序时,要考虑程序的强壮性,熟练掌握通过函数参数返回函数结果的办法。

实验内容:

编程实现顺序表下教材第二章定义的线性表的基本操作,最好用菜单形式对应各个操作,使其编程一个完整的小软件。

参考界面

在这里插入图片描述

验收/测试用例:

通过菜单调用各个操作,测试点:

  • 没有初始化前进行其他操作,程序是否能控制住;
  • 初始化一个顺序表;
  • 插入数据(位置, 数据),要测插入位置不合法的情况(0,1)、(2,1),正确插入4个数据(1,2)、(1,1)、(3,3);
  • 显示顺序表中的数据,屏幕输出1, 2, 3;
  • 判空,屏幕输出顺序表非空;
  • 顺序表长度,屏幕输出3;
  • 获取指定位置元素,要测指定位置在【1,3】范围之外的情况和之内的情况;
  • 定位,输入:4, 输出:不存在,输入2,输出位置为2;
  • 求直接前驱,要测求第一个元素的前驱、不存在顺序表中的元素的直接前驱,其他元素的直接前驱;
  • 求直接后继,要测最后一个元素的后继、不存在顺序表中的元素的直接后继,其他元素的直接后继;
  • 删除,要测位置在【1,3】范围之外的情况和之内的情况;
  • 清空操作后再测长度;
  • 销毁顺序表
#include<iostream>
using namespace std;

typedef int ElemType;
const int MAXSIZE = 100;

struct SqList{
	ElemType *elem; //数组动态分配 
	int length; //当前表长 
	
};

bool InitList(SqList &L); //1
void DestroyList(SqList &L);//2
void ClearList(SqList &L);//3
bool IsEmpty(SqList L);//4
int ListLength(SqList L);//5
int LocateElem(SqList L,int i,int e);//6
int PriorElem(SqList L,int cur_e,int &pre_e);//7
int NextElem(SqList L,int cur_e,int &next_e);//8
bool ListInsert(SqList &L,int i,int e);//9
bool ListDelete(SqList &L,int i,int &e);//10
void PrintList(SqList L);//11

int main(){
	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 << "       退出,输入一个负数!" << endl;
	SqList L;
	int n;
	bool flag = true;
	bool init = false;
	while(flag){
		cout << "请输入操作代码:";
	    cin >> n;
	    //cout << endl;
	    if(n == 1){
	    	init = true;
		}
	    if(n < 0){
	    	flag = false;
	    	break;
		}
		if(init==false){
			cout << "请先初始化!" <<endl;
		}
		int cur_e = 0,pre_e = 0,next_e = 0,i=0,e=0;
	    switch(n){
	    	case 1:{
	    		bool ans = InitList(L);
	    		if(ans){
	    			cout << "初始化完毕!" << endl;
				}
				else{
					cout << "初始化失败!" << endl;
				}
				break;
			}
			    
		    case 2:{
		    	if(init == true){
		    		DestroyList(L);
		    	    cout << "线性表已销毁!" << endl;
				    break;
		    		
				}
		    	
			}
		    case 3:{
		    	if(init == true){
		    		ClearList(L);
		    	    cout << "线性表已清空!" << endl;
				    break;
		    		
				}
		    	
			}
		    case 4:{
		    	if(init == true){
		    		bool ans = IsEmpty(L);
		    	if(ans){
		    		cout << "线性表是空的!" << endl; 
				}
				else{
					cout << "线性表非空!" <<endl;
				}
				break;
		    		
				}
		    	
			}
		    case 5:{
		    	if(init == true){
		    		int result = ListLength(L);
		    	    cout << "线性表长度为:" << result << endl;
				    break;
		    		
				}
		    	
			}
		    case 6:{
		    	if(init == true){
		    		cout << "请输入元素位置:" ;
		    	    cin >> e ;
		    	    if(e < 1 || e > L.length){
		    	    	cout << "该位置不合法,操作失败!" << endl;
		    	    	break;
					}
		    	    int ans = LocateElem(L,i,e); 
		    	    cout << "该位置元素是:" << ans << endl;
				    break;
		    		
				}
		    	
			}
		    case 7:{
		    	if(init == true){
		    		cout << "请输入元素位置:" ;
				    cin >> cur_e; 
		    	    int pre = PriorElem(L,cur_e,pre_e);
		    	    if(pre == 0){
		    	    	cout << "前驱不存在!" << endl;
		    	    	break;
					}
		    	    cout << "前驱是:" << pre << endl; 
				    break;
				}
		    	
		    	
			}
		    case 8:{
		    	if(init == true){
		    		cout << "请输入元素位置:";
		    	    cin >> cur_e;
		    	    if(cur_e <= 0 || cur_e >= L.length){
				    	cout << "后继不存在!" << endl;
				    	break;
					}
		    	    int next = 0;
				    next = NextElem(L,cur_e,next_e);
		    	    cout << "后继是:" << next << endl; 
				    break;
		    		
				}
		    	
			}
		    	
		    case 9:{
		    	if(init == true){
		    		cout << "请输入元素插入位置:";
		    	    cin >> i;
		    	    cout << "请输入元素的值:";
		    	    cin >> e;
		    	    bool ans = true;
				    ans = ListInsert(L,i,e);
		    	    if(ans){
		    		    cout << "元素插入成功!" << endl;
				    }else{
					    cout << "元素插入失败!" << endl;
				    }
				    break;
		    		
				}
		    	
			}
		    case 10:{
		    	if(init == true){
		    		cout << "请输入删除元素位置:";
		    	    cin >> i;
		    	    bool ans = ListDelete(L,i,e);
		    	    if(ans){
		    		    cout << "元素已删除!"  << endl; 
				    }else{
					    cout << "元素删除失败!" << endl;
				    }
				    break;
				}
		    	
			}
		    case 11:{
		    	if(init == true){
		    		PrintList(L);
				    break;
		    		
				}
		    	
			}
		    	
		    
	    }
		
	}
	
	
	return 0;
}
bool  InitList(SqList &L){//1----初始化一个线性表
	L.elem = new ElemType[MAXSIZE];
	if(!L.elem){
		return false;
	} 
	L.length = 0;
	return true;
	
}
void DestroyList(SqList &L){//2----销毁线性表
	if(L.elem){
		delete[] L.elem;
		L.length = 0;
		L.elem = NULL;
	}
	
}
void ClearList(SqList &L){//3----清空线性表
	L.length = 0;
	
}
bool IsEmpty(SqList L){//4----判断线性表是否为空
	if(L.length == 0){
		return true;
	}
	else{
		return false;
	}
	
}
int ListLength(SqList L){//5----求线性表长度
	return L.length;
	
}
int LocateElem(SqList L,int i,int e){//6----获取线性表指定位置元素
	for(i = 0; i < L.length; i++){
		if(i == e-1){
			return L.elem[i];
		}
	}
	return 0;
}
int PriorElem(SqList L,int cur_e,int &pre_e){//7----求前驱
	if(cur_e == 1){
		return 0;
	}
	if(cur_e > L.length || cur_e < 1){
		return 0;
	}
	for(int i = 0; i < cur_e; i++){
		if(i == cur_e - 1){
			pre_e = L.elem[i-1];
		}
	}
	return pre_e;
	
}
int NextElem(SqList L,int cur_e,int &next_e){//8----求后继
	if(cur_e >= L.length || cur_e < 1){
		return 0;
	}
	for(int i = 0; i < cur_e; i++){
		if(i == cur_e-1){
			next_e = L.elem[i+1];
			break;
		}
	}
	return next_e;
	
}
bool ListInsert(SqList &L,int i,int e){//9----在线性表指定位置插入元素
	if(i < 1 || i > L.length+1){
		return false;
	}
	if(L.length == MAXSIZE){
		return true;
	}
	for(int j = L.length-1;j>=i-1;j--){
		L.elem[j+1] = L.elem[j];
	}
	L.elem[i-1]=e;
	++L.length;
	return true;
}
bool ListDelete(SqList &L,int i,int &e){//10----删除线性表指定位置元素
	if(i < 1 || i > L.length){
		return false;
	}
	for(int j = i; j <= L.length-1; j++){
		L.elem[j-1] = L.elem[j];
	}
	--L.length;
	return true;
}
void PrintList(SqList L){//11----显示线性表
	for(int i = 0; i < L.length; i++){
		if(i == L.length - 1){
			cout << L.elem[i] << endl;
			break;
		}
		cout << L.elem[i] << " ";
		
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值