数据结构定义
typedef int ElemType;
typedef struct
{
ElemType *data;
int length;
int capacity;
}SqList;
Elemtype
是数据元素的类型,这里定义为int
。SqList
是顺序表的数据结构,其中包含
data
:指向存储数据的动态数组的指针。length
:当前数据元素的数量。capacity
:数组的当前容量(即最大能存储的元素数量)。
初始化顺序表
void InitList(SqList &L, int initalLength, int initalCapacity, int arr[])
{
L.data = new ElemType[initalCapacity];
L.length = initalLength;
L.capacity = initalCapacity;
for (int i = 0; i < L.length; i++)
L.data[i] = arr[i];
}
L.data
动态分配一个数组,初始容量为initalCapacity
。L.length
设置顺序表的长度为initalLength,即当前元素个数。L.capacity
设置为initalCapacity
,即当前可以存储的最大元素数量。- 通过循环将传入的数组arr赋值给顺序表的data数组。
销毁顺序表
void DestoryList(SqList& L)
{
delete[] L.data;
L.data = nullptr;
L.length = 0;
L.capacity = 0;
}
delete[] L.data
释放动态分配的内存。L.data
设为nullptr
,防止悬挂指针。L.length
和L.capacity
为0。
扩展顺序表
void ExpandList(SqList &L)
{
int newCapacity = L.capacity * 2;
ElemType *newData = new ElemType[newCapacity];
for (int i = 0; i < L.length; i++)
newData[i] = L.data[i];
delete[] L.data;
L.data = newData;
L.capacity = newCapacity;
}
newCapacity
是新的容量,通常是原来的两倍,这里有点像vector的扩容机制。- 创建一个新的动态数组
newData
。 - 把原数组的内容复制到新数组
newData
中。 - 释放原数组的内存,并且把
L.data
指向新数组。 - 更新
L.capacity
。
插入元素
bool ListInsert(SqList &L, int index, ElemType value)
{
if (index < 1 || index > L.length + 1)
return false;
if (L.length >= L.capacity)
ExpandList(L);
for (int i = L.length; i >= index; i--)
L.data[i] = L.data[i - 1];
L.data[index - 1] = value;
L.length++;
return true;
}
- 检查
index
是否有效,如果无效直接返回false
。 - 如果当前数组满了,调用
ExpandList
扩容。 - 将插入位置及其后面的元素向后移动一位。
- 将新元素插入指定位置,更新
L.length
。
删除元素
bool DeleteElem(SqList &L, int index, ElemType &value)
{
if (index < 1 || index > L.length)
return false;
value = L.data[index - 1];
for (int i = index; i < L.length; i++)
L.data[i - 1] = L.data[i];
L.length--;
return true;
}
- 检查
index
是否有效。 - 保存被删除的数据到
value
变量中。 - 将删除位置后到元素往前移动一位。
- 更新
L.length
。
修改元素
bool ModifyList(SqList &L, int index, ElemType newValue)
{
if (index < 1 || index > L.length)
return false;
L.data[index - 1] = newValue;
return true;
}
- 检查
index
是否有效。 - 更新指定位置的值为
newValue
。
查看元素
void DisplayList(SqList L)
{
for (int i = 0; i < L.length; i++)
printf("%d ", L.data[i]);
printf("\n");
}
- 打印顺序表中的所有元素
完整代码(可执行)
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct
{
ElemType *data;
int length;
int capacity;
}SqList;
void InitList(SqList &L, int initalLength, int initalCapacity, int arr[])
{
L.data = new ElemType[initalCapacity];
L.length = initalLength;
L.capacity = initalCapacity;
for (int i = 0; i < L.length; i++)
L.data[i] = arr[i];
}
void DestoryList(SqList& L)
{
delete[] L.data;
L.data = nullptr;
L.length = 0;
L.capacity = 0;
}
void ExpandList(SqList &L)
{
int newCapacity = L.capacity * 2;
ElemType *newData = new ElemType[newCapacity];
for (int i = 0; i < L.length; i++)
newData[i] = L.data[i];
delete[] L.data;
L.data = newData;
L.capacity = newCapacity;
}
bool ListInsert(SqList &L, int index, ElemType value)
{
if (index < 1 || index > L.length + 1)
return false;
if (L.length >= L.capacity)
ExpandList(L);
for (int i = L.length; i >= index; i--)
L.data[i] = L.data[i - 1];
L.data[index - 1] = value;
L.length++;
return true;
}
bool DeleteElem(SqList &L, int index, ElemType &value)
{
if (index < 1 || index > L.length)
return false;
value = L.data[index - 1];
for (int i = index; i < L.length; i++)
L.data[i - 1] = L.data[i];
L.length--;
return true;
}
bool ModifyList(SqList &L, int index, ElemType newValue)
{
if (index < 1 || index > L.length)
return false;
L.data[index - 1] = newValue;
return true;
}
void DisplayList(SqList L)
{
for (int i = 0; i < L.length; i++)
printf("%d ", L.data[i]);
printf("\n");
}
int main()
{
int arr[] = {2, 2, 0, 5, 2, 1, 8, 1};
int arrSize = sizeof(arr) / sizeof(arr[0]);
SqList list;
InitList(list, arrSize, 10, arr);
while (1)
{
cout << "==========================" << endl;
cout << "1. 插入数据" << endl;
cout << "2. 删除数据" << endl;
cout << "3. 修改数据" << endl;
cout << "4. 查看数据" << endl;
cout << "5. 退出" << endl;
cout << "==========================请输入:";
int input = 0;
cin >> input;
if (input == 1)
{
int index = 0;
int value = 0;
cout << "=====请输入要插入的位置:";
cin >> index;
cout << "=====请输入要插入的数据元素:";
cin >> value;
if (ListInsert(list, index, value))
{
cout << "插入后的数据是:";
DisplayList(list);
}
else cout << "插入失败" << endl;
}
if (input == 2)
{
int index = 0;
int value = 0;
cout << "=====请输入要删除的位置:";
cin >> index;
if (DeleteElem(list, index, value))
{
cout << "删除后的数据是:";
DisplayList(list);
}
else cout << "删除失败" << endl;
}
if (input == 3)
{
int index = 0;
int newvalue = 0;
cout << "=====请输入要修改的位置:";
cin >> index;
cout << "=====请输入新的数据元素:";
cin >> newvalue;
if (ModifyList(list, index, newvalue))
{
cout << "修改后的数据是:";
DisplayList(list);
}
else cout << "修改失败" << endl;
}
if (input == 4) DisplayList(list);
if (input == 5) break;
}
DestoryList(list);
return 0;
}