Type definition of linear table

Linear table (linear_list) is the most common and simplest data structure. In short, a linear table is a finite sequence of n data elements. As for the specific meaning of each data element, it is different in different situations. It can be a number of symbols, a page, or even other more complex information. For example, the 26 English alphabets are a linear table, and the data elements in the table are single alphabetic characters.

(A,B,C,...,Z)

As another example, the change of the number of computers owned by a primary school from 2000 to 2020 can be given in the form of a linear table, and the data elements in the table are integers

(6,12,23,34,45,...,99)

In a slightly more complicated linear table, a data element can be composed of several data items . In this case, the data element is often called a record , and a linear table with a large number of records is also called a file.
For example, a school ’s student health status rating table. Each student ’s condition in the table is a record. The six data items are student number, gender, age, class and health status.

Insert picture description here

In summary, the data elements in the linear table can be various, but the elements in the same linear table must have the same characteristics, that is, belong to the same data object, and there is an order-even relationship between adjacent data elements. If the linear table is written as,
Insert picture description here
then ai-1 is ahead of ai and ai is ahead of ai + 1. Ai-1 is said to be the direct precursor of ai, and ai + 1 is the direct successor of ai. When i = 1,2, ··, n-1, ai has one and only one direct successor, when i = 2,3, ··, n, ai has one and only one direct precursor.

注意:除第一个之外,集合中的每个数据元素均只有一个前驱; 
	 除最后一个之外,集合中每个数据元素均只有一个后继;

The number of elements in the linear table n (n> = 0) is defined as the length of the linear table. When n = 0, it is called an empty table. Each element in the non-empty table has a definite position, such as a1 is the first data element, an is the last data element, ai is the ith data element, and i is called the data element ai in the linear table Bit order

The linear table is a fairly flexible data structure, and its length can be increased or shortened as needed, that is, not only can the abstract elements of the linear table be accessed, but also inserted and deleted.

ADT List {
 数据对象:D={ | ∈ ElemSet, i=1,2,...,n, n≥0 }
数据关系:R1={ <ai-1 ,ai >| ,∈D, i=2,...,n }

基本操作:
  {结构初始化}
  InitList( &L )
   操作结果:构造一个空的线性表 L 。

{销毁结构}
  DestroyList( &L )
   初始条件:线性表 L 已存在。
   操作结果:销毁线性表 L 。
例如,26个小写英文字母是一个线性表
     (a,b,,z)
同一花色的13张扑克牌
  (2,3,4,5,6,7,8,9,10,J,Q,K,A)
可以构成一个线性表。
序偶 <,> 表示 是 的直接前驱,反之, 是 的直接后继。
 {引用型操作}
  ListEmpty( L )
   初始条件:线性表L已存在。
   操作结果:若 L 为空表,则返回 TRUE,否则返回 FALSE。

ListLength( L )
   初始条件:线性表 L 已存在。
   操作结果:返回 L 中元素个数。
PriorElem( L, cur_e, &pre_e )
   初始条件:线性表 L 已存在。
   操作结果:若 cur_e 是 L 中的数据元素,则用 pre_e 返回它的前驱,
        否则操作失败,pre_e 无定义。
NextElem( L, cur_e, &next_e )
   初始条件:线性表 L 已存在。
   操作结果:若 cur_e 是 L 中的数据元素,则用 next_e 返回它的后继,
        否则操作失败,next_e 无定义。

GetElem( L, i, &e )
   初始条件:线性表 L 已存在,1≤i≤LengthList(L)
   操作结果:用 e 返回 L 中第 i 个元素的值。
LocateElem( L, e, compare( ) )
   初始条件:线性表 L 已存在,compare( ) 是元素判定函数。
   操作结果:返回 L 中第1个与 e 满足关系 compare( ) 的元素的位序。
        若这样的元素不存在,则返回值为0

 ListTraverse(L, visit( ))
  初始条件:线性表 L 已存在,visit( ) 为元素的访问函数。
  操作结果:依次对 L 的每个元素调用函数 visit( )
       一旦 visit( ) 失败,则操作失败。
{加工型操作}

ClearList( &L )
   初始条件:线性表 L 已存在。
   操作结果:将 L 重置为空表。

 PutElem( &L, i, &e )
   初始条件:线性表L已存在,1≤i≤LengthList(L)
   操作结果:L 中第 i 个元素赋值同 e 的值。
 ListInsert( &L, i, e )
   初始条件:线性表 L 已存在,1≤i≤LengthList(L)+1
   操作结果:在 L 的第 i 个元素之前插入新的元素 e,L 的长度增1

 ListDelete( &L, i, &e )
   初始条件:线性表 L 已存在且非空,1≤i≤LengthList(L)
   操作结果:删除 L 的第 i 个元素,并用 e 返回其值,L 的长度减1

} ADT List

For details, please refer to https://blog.csdn.net/m57091003/article/details/1911982

For the above-defined abstract data type linear table, you can also perform some more complex operations, for example, merge two or more linear tables into a linear table; split a linear table into two or more Linear table; re-copy a linear table, etc.

Example:

假设利用两个线性表LA和LB分别表示两个集合A和B(即线性表中的数据元素即为集合中的成员),
现要求一个新的集合A = A U B。这就要求对线性表做如下操作:

扩大线性表LA,将存在于线性表LB中依次取得每个数据元素,并依次在线性表LA中进行查看,若不存在,则插入。
void union(List &La, List Lb){
	//将所有在线性表Lb但不在La中的数据元素插入到La中
	La_len = ListLength(La);
	Lb_len = ListLength(Lb);	//求线性表的长度
	for(i=1;i<=Lb_len;i++){
		GetElem(Lb,i,e);	//求Lb中第i个数据元素赋给e
		if(!LocateElem(La,e,equal)
			ListInsert(La,++La_len,e);
				//La中不存在和e相同数据元素,则插入
	}
}//union

Example:

已知线性表LA和LB中国的数据元素按值递增有序排列,现要求将LA和LB归并为一个新的线性表LC,
且LC中的数据元素仍按值递增排列。
LA = (3,5,8,11)
LB = (2,6,8,9,11,25,20)
LC = (2,3,5,6,8,8,9,11,11,15,20)

From the above requirements, we can see that the data elements in the LC or the data elements in the LA or the data elements in the LB, as long as the LC is an empty table, and then the elements in the LA or LB are inserted into the LC one by one. In order to arrange the elements in the LC in increasing order of value, two pointers i and j can be set to point to certain elements in LA and LB. If the element currently pointed to by i is a and the element currently pointed to by b is b, then The element c that should be inserted into the LC is
Insert picture description here

void MegeList(List La, List Lb, List &Lc){
	//已知线性表La和Lb中的数据元素按值递增排列
	//Lc也呈递增排雷
	InitList(Lc);
	i = j =1;
	k = 0;
	La_len = ListLength(La);
	Lb_len = ListLength(Lb);
	while( (i<=La_len) && Lb_len = ListLength(Lb) ){	//La和Lb均非空
		GetElem(La, i, ai);
		GetElem(Lb, j, bi);
		if(ai <= bj){
			ListInsert(Lc, ++k, ai);
			++i;}
		else{
			ListInsert(Lc,++k,bj);
			++j;}
	}
	while(i <= La_len){
		GetElem(La, i++, ai);
		ListInsert(Lc, ++k, ai);
	}
	while(j <= Lb_len){
		GetElem(Lb, j++, bj);
		ListInsert(Lc, ++k, bi);
	}
}

The time complexity of the above two algorithms depends on the execution time of the basic operations in the definition of the abstract data type List. Assuming that the execution time of the two operations GetElem and ListInsert has nothing to do with the table length, the execution time of LocateElem is proportional to the table length, then the time replication of the previous algorithm is O (ListLength (LA) * ListLength (LB)), The time complexity of this algorithm is O (ListLength (LA) + ListLength (LB)). Although the latter algorithm has three while loop statements, only when i and j point to the data elements that actually exist in the table, the values ​​of the data elements can be obtained and compared; and when the data elements of one of the linear tables have been inserted into the linear After the table LC, just insert the remaining elements in another linear table in sequence. Therefore, for each set of specific inputs (LA and LB), the last two while loop statements execute only one loop body.

Published 71 original articles · Like 3 · Visits 4044

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/105322678