概述
一共三个文件,分别是SeqList.h
,SeqList.cpp
,main.cpp
,分别为声明头文件,实现源文件,和测试源文件。
该顺序表采用的是动态分配内存的设计,容量不足时会自动扩容为原来的2倍,但没有进行缩容操作的设计。
打印版和源文件版本的代码下载链接:下载传送门
// SeqList.h
#ifndef SEQLIST_SEQLIST_H
#define SEQLIST_SEQLIST_H
typedef int ElemType; // 数据类型
const int InitSize = 100; // 表的初始长度
typedef struct {
ElemType *data; //动态分配的数组指针
int capacity, length; // 数组的最大容量和当前长度
}SeqList;
bool InitList(SeqList &L); // 初始化表,构造一个空的顺序表
int Length(SeqList L); // 求表长
int LocateElem(SeqList L, ElemType e); // 按值查找, 范围元素的位置
ElemType GetElem(SeqList L, int i); // 按位置查找,获取表中第i个元素的值
bool ListInsert(SeqList &L, int i, ElemType e); // 插入操作,在第i个位置插入元素e
bool ListDelete(SeqList &L, int i, ElemType &e); // 删除操作,删除第i个位置的元素
void PrintList(SeqList L); // 输出操作,打印整个顺序表
bool Empty(SeqList L); // 判断顺序表是否为空
bool DestroyList(SeqList &L); // 销毁操作,销毁顺序表,并释放申请的所有空间
#endif //SEQLIST_SEQLIST_H
// SeqList.cpp
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
// 初始化顺序表
bool InitList(SeqList &L) {
L.data = (ElemType*)malloc(sizeof(ElemType) * InitSize);
if(L.data == NULL) return false;
L.capacity = InitSize;
L.length = 0;
return true;
}
// 求表长
int Length(SeqList L) {
return L.length;
}
// 按值查找, 范围元素的位置
int LocateElem(SeqList L, ElemType e) {
for (int i = 0; i < L.length; ++i) {
if (L.data[i] == e) {
return i + 1;
}
}
return -1;
}
// 按位置查找,C语言不支持多返回值,这里约定错误返回-1。
ElemType GetElem(SeqList L, int i) {
if(i < 1 || i > L.length) return -1;
return L.data[i - 1];
}
// 插入操作
bool ListInsert(SeqList &L, int i, ElemType e) {
if(i < 1 || i > L.length + 1) return false; // 越界
if(L.length >= L.capacity) { // 容量不足,动态扩容
printf("触发扩容机制\n");
ElemType *tmp = (ElemType*)malloc(sizeof(ElemType) * (L.capacity * 2)); // 扩容为原来的2倍
for(int k = 0; k < L.length; ++k) tmp[k] = L.data[k];
free(L.data);
L.data = tmp;
L.capacity *= 2;
}
for(int k = L.length; k >= i; --k) {
L.data[k] = L.data[k - 1];
}
L.data[i - 1] = e;
++L.length;
return true;
}
// 删除操作
bool ListDelete(SeqList &L, int i, ElemType &e) {
if(i < 1 || i > L.length) return false;
e = L.data[i - 1];
for(int k = i; k < L.length; ++k) L.data[k - 1] = L.data[k];
--L.length;
return true;
}
// 输出操作
void PrintList(SeqList L) {
for(int i = 0; i < L.length; ++i)
printf("%d ", L.data[i]);
puts("");
}
// 判断顺序表是否为空
bool Empty(SeqList L) {
return L.length == 0;
}
// 销毁操作,销毁线性表,并释放申请的所有空间
bool DestroyList(SeqList &L) {
free(L.data);
L.length = 0;
L.capacity = 0;
return true;
}
// main.cpp
#include <cstdio>
#include <cstdlib>
#include "SeqList.h"
using namespace std;
int main() {
SeqList array;
InitList(array);
// 插入10个元素
for(int i = 0; i < 10; ++i) {
ListInsert(array, i + 1, i + 1);
}
// 表长
printf("表长为:%d\n", Length(array));
// 查找值为8的元素的位置
printf("值为8元素的位置是:%d\n", LocateElem(array, 8));
// 获取第6个位置上的元素
printf("第6个位置上的元素是:%d\n", GetElem(array, 6));
// 输出表
PrintList(array);
// 删除第5个位置上的元素
ElemType e;
ListDelete(array, 5, e);
printf("第5个位置上的元素值为%d\n", e);
PrintList(array);
// 判断是否为空表
printf("是否为空表:%d\n", Empty(array));
// 销毁整个顺序表
DestroyList(array);
printf("是否为空表:%d\n", Empty(array));
}
/*
输出结果:
表长为:10
值为8元素的位置是:8
第6个位置上的元素是:6
1 2 3 4 5 6 7 8 9 10
第5个位置上的元素值为5
1 2 3 4 6 7 8 9 10
是否为空表:0
是否为空表:1
*/