要求:
对顺序存储的线性表进行一些基本操作。主要包括:
(1)插入,删除,查找(查询指定的元素):
(2)显示数据
(3)更新:修改指定元素的数据
(4)数据文件的读写操作等。
# include<fstream>
# include<iostream>
using namespace std;
const int maxsize = 100;
void showmenu() {
cout << "*************************************************" << endl;
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 << "* 0.退出 *" << endl;
cout << "*************************************************" << endl;
}
template<class T>
class SqList {
private:
T data[maxsize];
int length;
public:
SqList();//无参构造函数
void xSqList(int n);//新建顺序表
bool insert(int i, T& x);//指定元素位置插入或者元素前插入
bool insert2(int i, T& x);//指定元素后插入
bool dele1(int i);//按序号删除指定元素
bool dele2(T x);//按内容删除指定元素
int serch2(T x);//知道数据元素,查找数据序号
int serch1(int x);//指定数据序号,查找数据元素
int modify(int x);//修改指定元素的数据
void ofile();//存储到文件中
void ifile();//读取文件
~SqList(){}
void show();
};
template<class T>
SqList<T>::SqList() {
data[0] = 0; //任意初始化
length = 0;
}
template<class T>
void SqList<T>::xSqList(int n) { //新建顺序表
if (n > maxsize)
throw"输入的n超过了最大可存储量(100)";
cout << "依次输入元素,每个元素以空格隔开:" << endl;
for (int i = 0; i < n; i++)
cin >> data[i];
length = n;
cout << "初始化完成......." << endl;
}
template<class T>
void SqList<T>::show() {
if (length == 0)
cout << "该表为空" << endl;
else {
for (int i = 0; i < length; i++)
cout << data[i] << " ";
cout << endl;
}
}
template<class T>
bool SqList<T>::dele1(int i) { //按序号删除指定元素,这里的序号指的是显示出来的数据的序号而不是数组的逻辑序号
if (length == 0)
{
cout << "该表为空,无法进行删除" << endl;
return false;
}
else if (i<1 || i>length)
{
cout << "删除的序列号超出或短于了该表的长度" << endl;
return false;
}
for (int j = i; j < length ; j++)
data[j - 1] = data[j];
length--;
return true;
}
template<class T>
bool SqList<T>::dele2( T x) { //按内容删除指定元素,这里只是删除一个指定元素,考虑到有多个的相同的元素,只删除第一个,从题目要求来看,输入应该是不同的数据
if (length == 0)
{
cout << "该表为空,无法进行删除" << endl;
return false;
}
for (int i = 0; i < length; i++)
{
if (data[i] == x)
{
int n;
n=serch2(x);
return dele1(n);
}
}
return false;
}
template<class T>
int SqList<T>::serch2(T x) { //知道数据元素,查找数据序号,不是数组下标
for (int i = 0; i < length; i++)
if (data[i] == x)
return i + 1;
return 0;
}
template<class T>
int SqList<T>::serch1(int x) { //知道数据序号,查找数据元素
if (length == 0)
{
cout << "该表为空,无法进行查找" << endl;
return 0;
}
else if (x<1 || x>length)
{
cout << "查找的序列号超出或短于了该表的长度" << endl;
return 0;
}
else
return data[x - 1];
}
template<class T>
int SqList<T>::modify(int x) {
cout << "请输入想要修改成的值" << endl;
T m;
cin >> m;
data[x - 1] = m;
cout << "修改成功......" << endl;
return 0;
}
template<class T>
bool SqList<T>::insert(int i, T& x) { //插入
if (length == 100)
{
cout << "表满,不能插入" << endl;
return false;
}
else if (i<1 || i>length)
{
cout << "插入的序列号超出或短于了该表的长度" << endl;
return false;
}
for (int j = length; j >= i; j--)
data[j] = data[j - 1];
data[i-1] = x;
length++;
return true;
}
template<class T>
bool SqList<T>::insert2(int i, T& x) {//插入2
if (length == 100)
{
cout << "表满,不能插入" << endl;
return false;
}
else if (i<1 || i>length)
{
cout << "插入的序列号超出或短于了该表的长度" << endl;
return false;
}
for (int j = length; j > i; j--)
data[j] = data[j - 1];
data[i ] = x;
length++;
return true;
}
template<class T>
void SqList<T>::ofile() {
ofstream f1("顺序表实验数据.txt", ios::out);
if (!f1) {
cout << "存储文件失败!" << endl; exit(1);
}
for (int i = 1; i < length; i++)
f1.write((char*)&data[i - 1], sizeof(data[i - 1]));
cout << "存储成功!" << endl;
f1.close();
}
template<class T>
void SqList<T>::ifile() {
ifstream f2("顺序表实验数据.txt", ios::binary);
if (!f2) {
cout << "打开文件失败!" << endl;
exit(1);
}
cout << "文件内容如下:" << endl;
for (int i = 1; !f2.eof(); i++)
{
f2.read((char*)&data[i - 1], sizeof(data[i - 1]));
}
for (int j = 1; j <= length; j++)
cout << "#" << j << ":" << data[j - 1] << endl;
f2.close();
}
int main() {
SqList<int>sqlist1;
int n;
while (true) {
showmenu();
cin >> n;
switch (n)
{
case 1: {
int m;
cout << "请输入想要创建的顺序表长度" << endl;
cin >> m;
sqlist1.xSqList(m);
system("pause");
system("cls");
break;
}
case 2: { //指定元素前插入
int m;
cout << "请输入指定的元素" << endl;
cin >> m;
int y;
cout << "请输入需插入的元素" << endl;
cin >> y;
int n = sqlist1.serch2(m);
if (sqlist1.insert(n, y))cout << "插入成功" << endl;
else
cout << "插入失败......" << endl;
system("pause");
system("cls");
break;
}
case 3: { //指定元素位置插入
int k;
cout << "请输入需要插入的序号" << endl;
cin >> k;
int h;
cout << "请输入需要插入的元素" << endl;
cin >> h;
if (sqlist1.insert(k, h))cout << "插入成功" << endl;
else
cout << "插入失败......" << endl;
system("pause");
system("cls");
break;
}
case 4: { //指定元素后插入
int m;
cout << "请输入指定的元素" << endl;
cin >> m;
int y;
cout << "请输入需插入的元素" << endl;
cin >> y;
int n = sqlist1.serch2(m);
if (sqlist1.insert2(n, y))cout << "插入成功" << endl;
else
cout << "插入失败......" << endl;
system("pause");
system("cls");
break;
}
case 5: {
int m;
cout << "请输入想要删除的元素值" << endl;
cin >> m;
if (sqlist1.dele2(m))cout << "元素为" << m << "的数据已经删除" << endl;
else cout << "删除失败" << endl;
system("pause");
system("cls");
break;
}
case 6: {
int n;
cout << "请输入想要删除数据的序号" << endl;
cin >> n;
if (sqlist1.dele1(n))cout << "第" << n << "元素删除完毕" << endl;
else cout << "删除失败" << endl;
system("pause");
system("cls");
break;
}
case 7: {
sqlist1.show();
system("pause");
system("cls");
break;
}
case 8: {
int m;
cout << "请输入需要查找的指定序号" << endl;
cin >> m;
int n=sqlist1.serch1(m);
if (n == 0)cout << "查找失败" << endl;
else
cout << "序号为" << m << "的元素值为" << n << endl;
system("pause");
system("cls");
break;
}
case 9: {
int m;
cout << "请输入想要查找的元素值" << endl;
cin >> m;
int n=sqlist1.serch2(m);
if (n == 0)cout << "没有找到该元素" << endl;
else
cout << "该元素的序号为:"<<n<< endl;//不是下标号
system("pause");
system("cls");
break;
}
case 10: {
int m;
cout << "请输入想要修改的元素" << endl;
cin >> m;
int n=sqlist1.serch2(m);
if (n == 0)cout << "表中没有该元素" << endl;
else
sqlist1.modify(n);
system("pause");
system("cls");
break;
}
case 11: {
sqlist1.ofile();
system("pause");
system("cls");
break;
}
case 12: {
sqlist1.ifile();
system("pause");
system("cls");
break;
}
case 0: {
return 0;
break;
}
}
}
}
测试效果: