C++ 顺序表的实现
#include <iostream>
using namespace std;
#define elementType int
/* 手搓顺序表 */
struct SequenceList
{
elementType* elements; // 元素 -- 指向 int 数组类型的指针
int size; // 实际长度
int capacity; // 最大长度
};
/* 顺序表的初始化 */
void initialize(SequenceList* list, int capacity)
{
list->elements = new elementType[capacity]; // 开辟一块内存空间给顺序表
list->size = 0; // 初始化时长度为 0
list->capacity = capacity;
}
/* 顺序表的销毁 */
void destroy(SequenceList* list)
{
delete[] list->elements; // 释放顺序表中 elements 指向的内存块
}
/* 获取顺序表长度 */
int getSize(SequenceList* list)
{
// -> 表示获取结构体指针的成员变量
// . 表示获取结构体变量的成员变量
return list->size;
}
/* 判断顺序表是否为空 */
bool isEmpty(SequenceList* list)
{
return list->size == 0;
}
/* 顺序表中插入元素 */
void insert(SequenceList* list, int index, elementType element)
{
// 1.插入位置不合法的处理
if (index < 0 || index > list->size)
{
throw std::invalid_argument("Ininvalid index...");
}
// 2.顺序表已满的处理
if (list->size == list->capacity)
{
// 产生一个翻倍的容量
int newCapacity = list->capacity * 2;
// 根据新容量,给新数组动态分配更大的内存空间
elementType* newElements = new elementType[newCapacity];
// 将旧数组内容复制到新数组
for (int i = 0; i < list->size; i++)
{
newElements[i] = list->elements[i];
}
// 释放旧数组内存空间
delete[] list->elements;
// 将新数组作为结构体的元素数组
list->elements = newElements;
// 更新结构体的最大容量
list->capacity = newCapacity;
}
// 3.正常的插入情况
for (int i = list->size - 1; i >= index; i--)
{
// 一定要从后往前操作,每次将对应的元素后移一位
list->elements[i + 1] = list->elements[i];
}
// 插入元素
list->elements[index] = element;
// 4.插入完成后,顺序表长度加一
list->size++;
}
/* 根据索引删除顺序表元素 */
void deleteByIndex(SequenceList* list, int index)
{
// 1.删除位置不合法的处理
if (index < 0 || index >= list->size)
{
throw std::invalid_argument("Ininvalid index...");
}
// 2.正常的删除情况
for (int i = index; i < list->size - 1; i++)
{
// 从前往后操作,依次用下一个元素覆盖当前元素
list->elements[i] = list->elements[i + 1];
}
// 3.删除完成后,更新顺序表长度
list->size--;
}
/* 在顺序表中查找元素并返回索引 */
int getByIndex(SequenceList* list, elementType element)
{
// 1.正常情况
for (int i = 0; i < list->size; i++)
{
if (list->elements[i] == element) {
return i;
}
}
// 2.没找到
return -1;
}
/* 根据索引返回顺序表中元素 */
elementType getElememt(SequenceList* list, int index)
{
// 1.索引非法
if (index < 0 || index >= list->size)
{
throw std::invalid_argument("Ininvalid index...");
}
// 2.正常情况
return list->elements[index];
}
/* 修改顺序表中元素 */
void update(SequenceList* list, int index, elementType value)
{
// 1.索引非法
if (index < 0 || index >= list->size)
{
throw std::invalid_argument("Ininvalid index...");
}
// 2.正常修改
list->elements[index] = value;
}
/* 打印顺序表 */
void display(SequenceList* list)
{
for (int i = 0; i < list->size; i++)
{
cout << list->elements[i] << " ";
}
cout << endl; // 浅换一个行
}
/* 主函数 */
int main()
{
// 定义一个顺序表
SequenceList myList;
// 初始化顺序表
initialize(&myList, 10);
cout << "Initialization result: ";
display(&myList);
cout << "Current length: " << getSize(&myList) << endl;
cout << "Is Empty: " << isEmpty(&myList) << endl;
// 插入元素
for (int i = 0; i < 10; i++)
{
insert(&myList, i, i * 10);
}
cout << "Insert element result: ";
display(&myList);
// 修改元素
cout << "Modify element result: ";
update(&myList, 1, 100);
display(&myList);
// 删除元素
cout << "Delete element result: ";
deleteByIndex(&myList, 0);
display(&myList);
// 查询元素
cout << "Search by index result: " << getElememt(&myList, 5) << endl;
cout << "Search by element result: " << getByIndex(&myList, 60) << endl;
// 销毁顺序表
cout << "Current length: " << getSize(&myList) << endl;
cout << "Is Empty: " << isEmpty(&myList) << endl;
destroy(&myList);
cout << "Search by index result: " << getElememt(&myList, 5) << endl;
cout << "Search by element result: " << getByIndex(&myList, 60) << endl;
cout << "Current length: " << getSize(&myList) << endl;
cout << "Is Empty: " << isEmpty(&myList) << endl;
return 0;
}