一、新建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;
}
四、最重效果