线性表(Linear List)是由n(n≥0)个数据元素(结点)a1,a2,…,an组成的有限序列。
① 数据元素的个数n定义为表的长度(n=0时称为空表)。
② 将非空的线性表(n>0)记作:(a1,a2,…,an)
③ 数据元素ai(1≤i≤n)只是个抽象符号,其具体含义在不同情况下可以不同。
线性表的逻辑结构特征
对于非空的线性表:
① 有且仅有一个开始结点a1,没有直接前趋,有且仅有一个直接后继a2;
② 有且仅有一个终结结点an,没有直接后继,有且仅有一个直接前趋an-1;
③ 其余的内部结点ai(2≤i≤n-1)都有且仅有一个直接前趋ai-1和一个ai+1。
常见的线性表的基本运算
1. InitList(L)
构造一个空的线性表L,即表的初始化。
2. DestroyList(L)
消除一个线性表L。
3. ClearList(L)
清空一个线性表L。
4. ListEmpty( L)
判继线性表L是否为空表。
5. ListLength(L)
求线性表L中的结点个数,即求表长。
6. GetElem(L,i)
取线性表L中的第i个结点,这里要求1≤i≤ListLength(L)
7. LocateElem(L,x)
在L中查找值为x 的结点,并返回该结点在L中的位置。若L中有多个结点的值和x 相同,则返回首次找到的结点位置;若L中没有结点的值为x ,则返回一个特殊值表示查找失败。
8. PriorElem(L,cur_e,*pre_e)
返回L中的当前结点的上一个结点。
9. NextElem(L,cur_e,*pre_e)
返回L中的当前结点的下一个结点。
10. ListInsert(L,x,i)
在线性表L的第i个位置上插入一个值为x 的新结点,使得原编号为i,i+1,…,n的结点变为编号为i+1,i+2,…,n+1的结点。这里1≤i≤n+1,而n是原表L的长度。插入后,表L的长度加1。
11. ListDelete(L,i)
删除线性表L的第i个结点,使得原编号为i+1,i+2,…,n的结点变成编号为i,i+1,…,n-1的结点。这里1≤i≤n,而n是原表L的长度。删除后表L的长度减1。
12. ListTraverse( L,visit())
遍历线性表L。
/*************************line.h****************/
typedef int Status;
struct LIST{
Status NextElem(List L,ElemType cur_e,int *next_e)
ElemType *p,*q,*newbase;
|