C++实现顺序表相关操作
阅读原文时间:2023年07月08日阅读:1

//顺序表
#include
#include//C中stdlib.h动态分配内存
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int Elemtype;
//线性表类型定义
//顺序表的存储结构,结构体类型
typedef struct
{
Elemtype* elem;//动态开辟存储空间
int length;//当前数组的长度
}Sqlist;//顺序表结构类型为Sqlist
Status Initlist(Sqlist& L);//初始化顺序表
Status Createlist(Sqlist& L);//创建顺序表
Status Getlist(Sqlist L, int i, Elemtype& e);//顺序表取值
Status Locatelist(Sqlist L, Elemtype e);//顺序表查找值
Status Insertlist(Sqlist& L, Elemtype e, int m);//顺序表插入值
Status Deletelist(Sqlist& L, int i);//顺序表删除值
Status Printlist(Sqlist L);//顺序表打印
int main(void)
{
int k = 0;
Sqlist L;
do
{
cout << "*****顺序表相关操作******"; cout << "\n1.初始化顺序表"; cout << "\n2.创建顺序表"; cout << "\n3.顺序表取值"; cout << "\n4.顺序表查找值"; cout << "\n5.顺序表插入值"; cout << "\n6.顺序表删除值"; cout << "\n7.顺序表打印"; cout << "\n0.结束操作"; cout << "\n输入想要进行的操作数为:"; cin >> k;
switch (k) {
case 1:
{
if (Initlist(L))
cout << "初始化成功!" << endl; else cout << "初始化失败!" << endl; }break; case 2: { cout << "该顺序表长度length为:"; cin >> L.length;
if (Createlist(L))
cout << "创建成功!" << endl; else cout << "创建失败!" << endl; }break; case 3: { int a; cout << "想要取出顺序表值的位置为:"; cin >> a;
int e = 0;
if (Getlist(L, a, e))
cout << "取值成功!" << endl; else cout << "取值失败!" << endl; }break; case 4: { int b; cout << "想要查找顺序表元素为:"; cin >> b;
if (Locatelist(L, b))
cout << "查找成功!" << endl; else cout << "查找失败!" << endl; }break; case 5: { int d; cout << "想要插入顺序表的元素为:"; cin >> d;
int c;
cout << "想要插入顺序表元素的位置为:"; cin >> c;
if (Insertlist(L, d, c))
cout << "插入成功!" << endl; else cout << "插入失败!" << endl; }break; case 6: { int f; cout << "想要删除顺序表元素的位置为:"; cin >> f;
if (Deletelist(L, f))
cout << "删除成功!" << endl; else cout << "删除失败!" << endl; }break; case 7: { if (Printlist(L)) cout << "打印成功!" << endl; else cout << "打印失败!" << endl; }break; } } while (k != 0); return OK; } Status Initlist(Sqlist& L)//初始化顺序表 { L.elem = new Elemtype[MAXSIZE];//为顺序表分配一个数组空间 if (!L.elem) exit(OVERFLOW); L.length = 0; return OK; } Status Createlist(Sqlist& L)//创建顺序表 { int m; cout << "输入顺序表的数据:"; for (int i = 0; i < L.length; i++) { cin >> m;
L.elem[i] = m;
}
return OK;
}
Status Getlist(Sqlist L, int i, Elemtype& e)//顺序表取值
{
if (i<1 || i>L.length) return ERROR;
e = L.elem[i - 1];
cout << "顺序表取值为:" << e << endl; return OK; } Status Locatelist(Sqlist L, Elemtype e)//顺序表查找值 { for (int i = 0; i < L.length; i++) { if (L.elem[i] == e) { int k = 0; k = i + 1; cout << "顺序表查找元素位置在第" << k << "位"; return OK; } } return ERROR; } Status Insertlist(Sqlist& L, Elemtype e, int m)//顺序表插入值,e为插入元素,m为插入位置 { if (m<1 || m>(L.length + 1)) return ERROR;
if (L.length == MAXSIZE) return ERROR;
for (int i = L.length-1; i >= m - 1; i--)
{
L.elem[i + 1] = L.elem[i];
}
L.elem[m - 1] = e;
++L.length;
return OK;
}
Status Deletelist(Sqlist& L, int i)//顺序表删除值
{
int e = L.elem[i - 1];
for (int k = i; k < L.length; k++)
{
L.elem[i - 1] = L.elem[i];
}
--L.length;
return OK;
}
Status Printlist(Sqlist L)//顺序表打印
{
cout << "顺序表的值顺序输出为:";
for (int i = 0; i < L.length; i++)
{
cout << L.elem[i] << " ";
}
return OK;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器