#include<iostream>
using namespace std;
#define InitSize 10 //10不会直接写,会写的更通用一点
#define IncreaSize 2
typedef int ElemType; //typedef: 类型的重命名 struct --> SqList int --> ElemType
//使用结构体对顺序表进行表示
typedef struct
{
ElemType* data; //指向这个结构体的指针变量,存储空间动态申请
int length; //顺序表当前长度
int MaxSize; //顺序表最大长度
}SqList;
//1 初始化顺序表
bool InitList(SqList &L)
{
//首先需要动态分配一个地址
//注意这个函数参数的位置,这里可以写成SqList *L,下面必须写成L->elem或者(*L).elem,
//如果是Sqlist &L 下面写的形式是L.elem(*L 表示的是指针,而&L是引用,说人话就是二者都可以
// 指向这个顺序表,&L可以理解为一个别名而已。先这样理解吧)
L.data = new ElemType[InitSize];
if (L.data == NULL)
return false;
L.length = 0;
L.MaxSize = InitSize;
return true;
}
//2 向顺序表中存入数据
void Input(SqList&L, ElemType n)
{
if (n > InitSize || n <= 0)
cout << "输入错误,请重新输入:" << endl;
cout << "请输入" << n << "个数据" << endl;
for (int i = 0; i < n; i++)
{
cin >> L.data[i];
}
L.length = n;
}
//3 线性表的长度
ElemType GetLength(SqList& L)
{
return L.length;
}
//4 遍历顺序表中的数据
void Show(SqList& L)
{
cout << "顺序表内存储的数据为:" << endl;
for (int i = 0; i < L.length; i++)
{
cout << L.data[i] << " ";
}
cout << endl;
}
//5 向顺序表指定位置插入数据
bool InsertList(SqList& L, ElemType k, ElemType e)
{
if (L.length == InitSize)
cout << "顺序表已存满" << endl;
if (k<1 || k>L.length+1)
return false;
cout << "在顺序表第" << k << "个位置插入数据" << e << "后" << endl;
for (int i = L.length-1; i >= k-1; i--)
{
L.data[i+1] = L.data[i];
}
L.data[k-1] = e;
L.length++;
return true;
}
//6 删除线性表指定位置数据
bool DeleteList(SqList& L, ElemType k)
{
if (k<1 || k>L.length)
return false;
cout << "删除顺序表中第" << k << "个数据后" << endl;
for (int i = k-1; i < L.length; i++)
{
L.data[i] = L.data[i + 1];
}
L.length--;
return true;
}
//7 返回顺序表中第i个数据元素的值
void GetElem(SqList& L, ElemType i)
{
if (i > L.length || i <= 0)
cout << "输入错误,请重新输入:" << endl;
cout << "第" << i << "个数值为" << L.data[i - 1] << endl;
}
//8 返回查找数据在顺序表中的位置
void LocateList(SqList& L)
{
int e,f;
if (L.length == 0)
{
cout << "当前顺序表为空" << endl;
}
else
{
cout << "请输入查询的数值:" << endl;
cin >> e;
for (int i = 0; i < L.length; i++)
{
if (L.data[i] == e)
{
f = i;
break;
}
else
f = -1;
}
if (f == -1)
{
cout << "顺序表中不存在查询值" << endl;
}
else
cout << "查找数据在顺序表中的位置为" << f << endl;
}
}
//9 动态增加顺序表长度
void IncreaList(SqList&L)
{
ElemType* p = L.data;
L.data = new ElemType[InitSize + IncreaSize];
for (int i = 0; i < L.length; i++)
{
L.data[i] = p[i];
}
L.MaxSize = L.MaxSize + IncreaSize;
delete p;
}
//10 清空线性表中数据,重置为空表
bool ClearList(SqList& L)
{
if (L.length == 0)
{
cout << "顺序表为空" << endl;
}
for (int i = 0; i < L.length; i++)
{
L.data[i] = 0;
}
L.length = 0;
cout << "顺序表已清空" << endl;
return true;
}
//11 销毁线性表
bool DestoryList(SqList& L)
{
if (L.data != NULL)
{
free(L.data); //首先释放地址空间
L.data = NULL; //指针指向空
L.length = 0; //长度均变成0
cout << "顺序表已销毁" << endl;
return true;
}
else
{
cout << "顺序表不存在" << endl;
return false;
}
}
//12 合并两个顺序表
//待续...
int main()
{
SqList L;
InitList(L);
Input(L, 6);
Show(L);
InsertList(L, 3, 9999);
Show(L);
DeleteList(L, 2);
Show(L);
GetElem(L, 4);
LocateList(L);
ClearList(L);
Show(L);
DestoryList(L);
system("pause");
return 0;
}
04-14
09-17
1362
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-29
2099
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)