#include <iostream>
using namespace std;
#define MAXSIZE 10
#define ERROR 0
#define OK 1
typedef int DataType;
class SqList
{
public:
SqList();//1.初始化顺序表
~SqList();//删除顺序表
//CreatSqlList DispList ListLength isEmpty getElemByIndex getElemByElem InsertSqList DeletElem
void CreatSqlList(int n);//2.创建一个含有n个元素的顺序表
void DispList();//3.输出顺序表
int ListLength();//4.获得顺序表长度
bool isEmpty();//5.判断顺序表是否为空
DataType getElemByIndex(int i);//6.查找第i个位置的元素,并返回值
int getElemByElem(DataType e);//7.按元素直接查找,返回元素的位置
void InsertSqList(int i, DataType e);//8.在位置i插入元素e
void DeletElem(int i);//9.删除指定位置的元素
int DeletZhi(DataType e);//10.删除指定值的元素
void DeletAll();//11.删除所有元素
private:
DataType* data;//存放顺序表元素
int length;//顺序表的长度
};
//1.初始化顺序表、销毁顺序表
SqList::SqList()
{
cout << "SqList的构造函数调用" << endl;
data = new DataType[MAXSIZE];//堆区开辟一个一维数组,在析构函数里手动释放
length = 0;
}
SqList::~SqList()
{
cout << "SqList的析构函数调用" << endl;
delete[] data;
}
//2.//创建一个含有n个元素的顺序表
void SqList::CreatSqlList(int n)
{
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i + 1 << "个元素:";
cin >> data[i];
}
length = n;
}
//3.输出顺序表
void SqList::DispList()
{
if (length == 0)
{
cout << "表为空" << endl;
}
for (int i = 0; i < length; i++)
{
cout << data[i] << " ";
}
cout << endl;
}
//4.获取顺序表的长度
int SqList::ListLength()
{
return length;
}
//5.判断顺序表是否为空
bool SqList::isEmpty()
{
if (length == 0)
{
return true;
}
return false;
}
//6.查找第i个位置的元素,并返回值
DataType SqList::getElemByIndex(int i)
{
isEmpty();
if (i<1 || i>length)
{
cout << "位置有误" << endl;
return ERROR;
}
cout << "查找的元素为:" << data[i - 1] << endl;
return OK;
}
//7.按元素直接查找,返回元素的位置
int SqList::getElemByElem(DataType e)
{
if (length == 0)
{
cout << "表为空" << endl;
}
int i = 0;
while (i < length && data[i] != e)
{
i++;
}
if (i >= length)
{
cout << "未找到该元素" << endl;
return ERROR;
}
else
{
cout << "找到该元素,是第 " << i + 1 << " 个位置的元素" << endl;
return OK;
}
}
//8.在位置i插入元素e
void SqList::InsertSqList(int i, DataType e)
{
if (i<1 || i>length + 1)//这里 + 1 可以在尾部插入
{
cout << "插入位置有误" << endl;
}
for (int j = length; j >= i; j--)
{
data[j] = data[j - 1];
}
data[i - 1] = e;
length++;
}
//9.删除位置i的元素
void SqList::DeletElem(int i)
{
if (i<1 || i>length)
{
cout << "删除位置有误" << endl;
}
for (int j = i; j < length; j++)
{
data[j - 1] = data[j];
}
length--;
}
//10.按值删除元素
int SqList::DeletZhi(DataType e)
{
int i = 0;
while (data[i] != e && i < length)//当找到与e相等的元素跳出循环
{
i++;
}
if (i >= length)
{
cout << "顺序表没有此值,删除失败" << endl;
return ERROR;
}
else
{
for (int j = i + 1; j < length; j++)
{
data[j - 1] = data[j];
}
}
length--;
}
//11.删除所有元素
void SqList::DeletAll()
{
for (int i = length - 1; i > 0; i--)
{
data[i - 1] = data[i];
}
cout << "元素已全删除!" << endl;
length = 0;
}
int main()
{
SqList L;
int select = 0;
cout << "1.创建一个含有n个元素的顺序表" << endl;
cout << "2.输出顺序表" << endl;
cout << "3.获得顺序表长度" << endl;
cout << "4.判断顺序表是否为空" << endl;
cout << "5.查找第i个位置的元素,并打印出来" << endl;
cout << "6.按元素直接查找,返回元素的位置" << endl;
cout << "7.在位置i插入元素e" << endl;
cout << "8.删除指定位置的元素" << endl;
cout << "9.删除指定值的元素" << endl;
cout << "10.删除所有元素" << endl;
cout << "0.退出" << endl;
while (true)
{
cout << "请输入您的选择:" << endl;
cin >> select;
int j = 0;//表示位置j
DataType elem;//表示元素elem
switch (select)
{
case 1:
int n;
cout << "请输入顺序表的元素个数: ";
cin >> n;
if (n > MAXSIZE)
{
cout << "ERROR! 超过最大容量!" << endl;
break;
}
L.CreatSqlList(n);
break;
case 2:
L.DispList();
break;
case 3:
cout << "顺序表的长度:" << L.ListLength() << endl;
break;
case 4:
if (L.isEmpty() == 1)
{
cout << "顺序表为空" << endl;
}
else
{
cout << "顺序表不为空" << endl;
}
break;
case 5:
cout << "请输入要查找第几个位置的元素:" << endl;
cin >> j;
L.getElemByIndex(j);
break;
case 6:
cout << "请输入要查找的元素:" << endl;
cin >> elem;
L.getElemByElem(elem);
break;
case 7:
cout << "请输入要插入的位置:" << endl;
cin >> j;
cout << "请输入要插入的元素:" << endl;
cin >> elem;
L.InsertSqList(j, elem);
break;
case 8:
cout << "要删除元素的位置:" << endl;
cin >> j;
L.DeletElem(j);
break;
case 9:
cout << "要删除的具体元素:" << endl;
cin >> elem;
L.DeletZhi(elem);
break;
case 10:
cout << "确定删除所有元素吗?" << endl;
cout << "请选择:1.确定删除;2.取消删除" << endl;
cin >> j;
if (j == 1)
{
L.DeletAll();
}
break;
case 0:
system("pause");
exit(0);
default:
cout << "输入有误!" << endl;
break;
}
}
system("pause");//执行这句后才调用析构函数,销毁对象
return 0;
}
C++实现顺序表
最新推荐文章于 2024-08-22 23:21:41 发布