提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、线性表是什么?
- 线性表的定义
- 线性表的特点
- 线性表的优点
- 二、顺序表的使用步骤
- 存储定义
- 初始化
- 创建
- 遍历
- 插入
- 删除
- 取值
- 删除
- 用main函数调用相关的函数
前言
此文章是根据学校老师所讲所总结,使用的语言为c++。
提示:以下是本篇文章正文内容,下面案例可供参考
一、线性表是什么?
1.线性表的定义:
用数据元素的有限序列表示,一个线性表是n个具有相同特性的数据元素的有先序列,其是最基本,最简单,最常用的一种数据结构。其分为顺序存储结构和链式储存结构。
2.线性表的特点:
同一线性表中的元素必定具有相同的特性,元素关系为一一对应。
3.线性表的优点:
逻辑结构简单,便于实现和操作。
二、顺序表的使用步骤
1.存储定义
代码如下(示例):
typedef struct { //定义顺序表
ElemType *elem; //指向数据元素的基地址
int length; //当前长度
}SqList; //顺序表的结构类型为SqList
2.初始化
顺序表的初始化需要构建一个空的顺序表
代码如下(示例):
Status InitList(SqList& L) { //创建顺序表
L.listSize = 100;
L.elem = new ElemType[L.listSize]; //为顺序表分配空间
if (!L.elem) exit(1); //存储分配失败
L.length = 0; //空表长度为0
return OK;
}
3.创建
将数据元素储存到空的顺序表中:
代码如下(示例):
void InputList(SqList& L, int n) { //将元素储存到空的顺序表中
for (int i = 0; i < n; i++) {
cin >> L.elem[i];
L.length++;
}
}
4.遍历
将顺序表中的元素值输出
代码如下(示例):
void OutputList(SqList L) { //遍历输出
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
5.插入
表的长度也随之增加。
元素的插入时需要注意:
(1)判断插入位置i 是否合法(1<=i<=n+1)。
(2)判断顺序表的存储空间是否已满。
(3)将第n至第i 位的元素依次向后移动一个位置,空出第i个位置(i=n+1时无需移动)。
(4)将要插入的新元素e放入第i个位置。
(5)表长加1,插入成功返回OK。
算法分析:
若插入在尾结点之后,则根本无需移动(特别快);若插入在首结点之前,则表中元素全部后移(特别慢);若要考虑在各种位置插入(共n+1种可能)的平均移动次数,该如何计算?
顺序表插入算法的平均时间复杂度为O(n)
代码如下(示例):
Status ListInsert(SqList& L, int i, ElemType e) {// 插入元素
if (i < 1 || i > L.length + 1 || L.length == L.listSize) {//i值不合法或着当前存储空间已满
cout << "Insert failed: Invalid position or list is full." << endl;
return ERROR;
}
for (int j = L.length; j >= i; j--) {
L.elem[j] = L.elem[j - 1]; //插入位置及之后的元素后移
}
L.elem[i - 1] = e; //将新的元素e放在第i个位置
L.length++; //表长加1
cout << "Insert successful." << endl;
return OK;
}
6.删除
表的长度也随之减少
算法步骤:
1)判断删除位置i是否合法(合法值为1≤i≤n)。
(2)将第i+1至第n位的元素依次向前移动一个位置。
(3)表长减1,删除成功返回OK。
代码如下(示例):
Status ListDelete(SqList& L, int i) { //将线性表的L的第i个数据元素删除
if (i < 1 || i > L.length) { //i值不合法
cout << "Delete failed: Invalid position." << endl;
return ERROR;
}
for (int j = i; j < L.length; j++) {
L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素前移
}
L.length--; //表长减1
cout << "Delete successful." << endl;
return OK;
}
7.取值
获取线性表L中的某一个数据元素的内容
代码如下(示例):
Status GetElem(SqList L, int i, ElemType& e) { //取值
if (i < 1 || i > L.length) { //判断i值是否合理,若不合理,返回ERROR
cout << "Get element failed: Invalid position." << endl;
return ERROR;
}
e = L.elem[i - 1]; //第i-1的单元存储着第i个数据
cout << "Element at position " << i << " is: " << e << endl;
return OK;
}
8.查找
在线性表中查找值为e的数据元素
查找图示:(根据指定数据获取数据所在的位置)
代码如下(示例):
int LocateElem(SqList L, ElemType e) { //查找
for (int i = 0; i < L.length; i++) { //从
if (L.elem[i] == e) { //查找成功,返回序号i+1
return i + 1;
}
}
return 0; //查找失败
}
9.用main函数调用相关的函数
其main函数代码如下(示例):
int main() {
SqList L;
ElemType e;
int n, pos;
if (InitList(L) != OK) {
cout << "Failed to initialize the list." << endl;
return 1;
}
cout << "Enter the number of elements to insert: ";
cin >> n;
InputList(L, n);
cout << "The elements in the list are: ";
OutputList(L);
cout << "Enter the position and element to insert: ";
cin >> pos >> e;
if (ListInsert(L, pos, e) != OK) {
cout << "Insertion failed." << endl;
}
else {
cout << "The elements in the list after insertion are: ";
OutputList(L);
}
cout << "Enter the position to delete: ";
cin >> pos;
if (ListDelete(L, pos) != OK) {
cout << "Deletion failed." << endl;
}
else {
cout << "The elements in the list after deletion are: ";
OutputList(L);
}
cout << "Enter the position to get the element: ";
cin >> pos;
if (GetElem(L, pos, e) == OK) {
cout << "The element at position " << pos << " is: " << e << endl;
}
else {
cout << "Get element failed." << endl;
}
cout << "Enter the element to locate: ";
cin >> e;
pos = LocateElem(L, e);
if (pos) {
cout << "The element " << e << " is found at position " << pos << endl;
}
else {
cout << "The element " << e << " is not found in the list." << endl;
}
delete[] L.elem;
return 0;
}
三,顺序表的概述
1.顺序表(顺序存储结构)的特点
(1)利用数据元素的存储位置表示线性表中相邻数据元素之间的前后关系,即线性表的逻辑结构与存储结构一致
(2)在访问线性表时,可以快速地计算出任何一个数据元素的存储地址。因此可以粗略地认为,访问每个元素所花时间相等
这种存取元素的方法被称为随机存取法
2.顺序表的优点
3.顺序表的缺点
在插入、删除某一元素时,需要移动大量元素
浪费存储空间
属于静态存储形式,数据元素的个数不能自由扩充
(为了克服这一缺点可以使用链表)