此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢!
一、线性表(List)的定义:
- 线性表(List):零个或多个数据元素的有限序列。
(通常将线性表记作(a1,a2,…,an),n指表长。当n=0时,称该表为空表。同时注意此处下标是从1开始的。)
线性表包括顺序表和链表。二者的主要区别在于:
-在顺序表中,数据元素是用一段地址连续的存储单元依次存储的。
-而在链表中,结点之间地址不是连续的,是用指针“连接”起来的。
此处需要特别注意几点:
- 线性表是一种逻辑结构而不是物理结构,表示元素之间一对一相邻的关系。
- 线性表是有限的。
- 线性表中元素都是数据元素,每个元素都是单个元素。
- 线性表中元素都是相同类型的。
二、线性表的ADT:
线性表的数据对象集合为{a1,a2,…,an},每个元素的类型均为DataType。
其中,除了第一个元素a1外,每一个元素有且只有一个直接前驱元素;除最后一个元素an外,每一个元素有且只有一个直接后继元素。数据元素之间的关系是一对一的关系。
[ADT of lists.]
Operations:
InitList(*L): Set up an empty list. //初始化操作,建立一个空的线性表。
ListEmpty(L):(Bool type) If the list is empty, return true, else return false. //若线性表为空,返回true,否则返回false。
ClearList(*L): Empty the list. //线性表清空。
DestroyList(*L): Destroy the list and free the space it occupies. //销毁线性表,并释放其占用的存储空间。
GetElem(L,i,*e): (按位查找)Get the No.i element's value, and give it to "e". //将线性表L中第i个位置元素返回给e。
LocateElem(L,e): (按值查找)Search for an element that has the same value as "e", and return the element's location if finding it, else return 0. //在线性表L中查找与给定值e相等的元素,如果查找成功,返回该元素在表中的序列号;否则,返回0表示失败。
ListInsert(*L,i,e): Insert a new element "e" into the list right before No.i element. //在线性表的第i个位置插入元素e。
ListDelete(*L,i,*e): Delete the No.i element in the list, give its value to "e", and return "e". //删除线性表L中的第i个元素,并用e返回其值。
ListLength(L): Return the amount of the elements in the list. //返回线性表L的元素个数。
PrintList(L): Print the list. //打印线性表
end ADT
三、线性表的顺序存储结构(Sequential List)定义:
- 定义: 用一段地址连续的存储单元依次存储线性表的数据元素。
(Example: C’s one-dimensional array)
SqList的三个基本属性:
- The max size of a list.(1.线性表的最大存储容量。)
- An initial position of a list.(2.存储空间的起始位置。)
- The length of a list.(3.线性表的长度。)
接下来是线性表SqList的结构代码:
[The structure code of a Sequential List.]//线性表顺序存储的结构代码
//Here are three basic attributes of a SqList:
#define MAXSIZE 100 //No.1: The max size of a list.(1.线性表的最大存储容量。)
typedef struct
{
//ElemType为typedef的int类型数据。
ElemType data[MAXSIZE]; //No.2: An initial position of a list.(2.存储空间的起始位置。)
int length; //No.3: The length of a list.(3.线性表的长度。)
}SqList;
四、顺序表的部分操作具体实现方法:
首先,为了示意清楚以及方便,先声明定义:
#define OK 1
#define ERROR 0
typedef int ElemType; //ElemType的类型根据实际需求而定,此处假设为int。
typedef int Status;/*"Status" is defined as a type of a function, with its returned value representing the result of the function.
(Status是函数的类型,它的返回值势函数结果状态代码,0或1.)*/
- "GetElem"(获取元素): T(n) = O(1)
[P50: Achieve the operation of "GetElem" in a SqList.]//获取顺序存储结构链表中的某一元素
//INITIAL CONDITIONS : The list exists, and 1<=i<= ListLength(L).
//RESULT: Return the value of No.i element in the list, and give it to "e". 用e返回L中第i个元素的值。
Status GetElem ( SqList L, int i, ElemType *e )
{
if( L.length==0 || i<1 || i>L.length ) //Empty list or Index out of range is invalid. 空表/下标不在合法范围内。
{
return ERROR;
}
*e = L.data[i-1];
return OK;
}
T(n) = O(1)
- "ListInsert"(插入): T(n) = O(n)
[Achieve the operation of "ListInsert" in a SqList.]//向顺序存储结构链表中插入元素
//INITIAL CONDITIONS: The list exists, and 1<=i<=Listlength(L).
//RESULT: Insert "e" into the place right before No.i, and lenghthen the list for 1.(操作结果:在L中第i个位置前插入数据元素eÿ