利用 C++ 实现线性表的 插入、删除、遍历、查找等功能

一、新建sqlist.h头文件,在里面创建线性表类 

#pragma once
#include<iostream>
using namespace std;

#define MAXSIZE 100 //定义数组可能达到的最大长度


//定义线性表类
class SqList
{
public:

	//初始化
	SqList();

	//显示界面
	void MenuList();

	//在线性表中任意位置i,插入数据e
	void InsertList();

	//删除线性表中任意位置i中的数据
	void DeleteList();

	//根据位置i获取相应位置数据元素内容,并存储到变量e中
	void GetElem();

	//在线性表中查找值为e的元素,返回其序号(是第几个元素)
	void LocateElem();


	//遍历线性表中元素
	void PrintList();


	//清空线性表
	void ClearList();

	//销毁线性表并退出
	void DestoryList();


public:
	int* elem; //存储空间基地址,用来定义动态数组

	int lenght; //存储线性表长度


};


二、新建sqlist.cpp源文件,在里面实现具体操作

#include"sqlist.h"

//在构造函数中初始化
SqList::SqList()
{
	this->elem = new int[MAXSIZE];
	if (this->elem == NULL) //判断是否创建成功
	{
		cout << "创建失败!!!" << endl;
		exit(0);
	}
	this->lenght = 0;
}

//显示界面
void SqList::MenuList()
{
	cout << "-------------1.插入-------------" << endl;
	cout << "-------------2.删除-------------" << endl;
	cout << "-------------3.查找-------------" << endl;
	cout << "-------------4.遍历-------------" << endl;
	cout << "-------------5.清空-------------" << endl;
	cout << "-------------0.退出-------------" << endl;
}

//在线性表中任意位置i,插入数据e
void SqList::InsertList()
{
	int i, e;
	cout << "请输入你要插入的位置:" << endl;
	cin >> i;

	if (i < 1 || i > this->lenght + 1) //判断i是否合法
	{
		cout << "你输入的位置有误" << endl;

	}
	else if (this->lenght == MAXSIZE) //判断当前存储空间是否已满
	{
		cout << "存储空间已满" << endl;
	}
	else
	{
		cout << "请输入你要插入的元素:" << endl;
		cin >> e;
		for (int j = this->lenght - 1; j >= i - 1; j--)
		{
			this->elem[j + 1] = this->elem[j]; //插入位置及之后的元素后移
		}

		this->elem[i - 1] = e; //将新元素e放入位置i
		this->lenght++; //表长加1
		cout << "插入成功" << endl;
	}

	system("pause");
	system("cls");
}

//删除线性表中任意位置i中的数据
void SqList::DeleteList()
{
	int i;
	cout << "请输入你要删除的位置:" << endl;
	cin >> i;
	if ((i < 1) || (i > this->lenght))  //判断删除地址是否合法
	{
		cout << "你输入的位置有误" << endl;
	}
	else
	{
		for (int j = i; j < this->lenght; j++) { //需要删除的位置后面的元素往前覆盖,以实现删除操作
			this->elem[j - 1] = this->elem[j];
		}
		--this->lenght; //表长减一
		cout << "删除成功" << endl;
	}

	system("pause");
	system("cls");
}

//根据位置i获取相应位置数据元素内容,并存储到变量e中
void SqList::GetElem()
{
	int i, e;
	cout << "请输入要查找的位置:" << endl;
	cin >> i;
	if (i < 1 || i > this->lenght) //判断i的值是否合法
	{
		cout << "你输入的值有误" << endl;
	}
	else
	{
		e = this->elem[i - 1]; //将查找到的值存入变量e中
		cout << " i= " << e << endl;
	}
	system("pause");
	system("cls");
}

//在线性表中查找值为e的元素,返回其序号(是第几个元素)
void SqList::LocateElem()
{
	int e;
	cout << "请输入要查找的值:" << endl;
	cin >> e;
	for (int i = 0; i < this->lenght; i++)
	{
		if (this->elem[i] == e)
		{
			cout << " e= " << i + 1 << endl; //查找成功返回序号
		}
	}
	system("pause");
	system("cls");
}

//遍历线性表中元素
void SqList::PrintList()
{
	if (this->lenght == 0) //判断线性表是否为空
	{
		cout << "线性表为空" << endl;
	}
	else
	{
		for (int i = 0; i < this->lenght; i++) //遍历输出
		{
			cout << this->elem[i] << " ";
		}
		cout << endl;
	}

	system("pause");
	system("cls");
}

//清空线性表
void SqList::ClearList()
{
	this->lenght = 0; //将线性表的长度置为0
	cout << "清空成功" << endl;
	system("pause");
	system("cls");
}

//销毁线性表并退出
void SqList::DestoryList()
{
	if (!this->elem == NULL) //判断数组是否为空,不为空则销毁数组
	{
		delete[] this->elem;
		this->elem = NULL;
		cout << "销毁成功" << endl;
	}
	
	system("pause");
	system("cls");
}



三、利用switch、while等函数实现对线性表的操作

#include<iostream>
using namespace std;
#include"sqlist.h" //包含线性表类头文件


int main()
{
	SqList sl; //创建对象sl

	int select = 0; 
	int i = 0;

	while (true)
	{
		sl.MenuList();
		cout << "请输入您的选择:" << endl;
		cin >> select;
		switch (select)
		{
		case 1: //插入
			sl.InsertList();
			break;
		case 2: //删除
			sl.DeleteList();
			break;
		case 3: //查找
			cout << "请选择查找方式:1、按位置查找 2、按值查找" << endl;
			cin >> i;
			if (i == 1)
			{
				sl.GetElem();
			}
			else
			{
				sl.LocateElem();
			}
			break;
		case 4: //遍历
			sl.PrintList();
			break;
		case 5: //清空
			sl.ClearList();
			break;
		case 0: //退出
			sl.DestoryList();
			break;
		default:
			cout << "你输入的有误请重新输入:" << endl;
			system("pause"); //按任意键继续
			system("cls"); //清屏返回菜单界面
			break;
		}

	}

	
	
	system("pause");
	return 0;
}

四、最重效果

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值