Data Structure Notes (1) the linear form

2.1 Definition and characteristics of the linear form

Linear structure: Ordered a data element (order) sequence.
Basic Features:

  • Exists only one is referred to as "a first" element of the data;
  • Exists only one is referred to as the "last" data element;
  • Except the first one, each data element in the collection are only a precursor;
  • Except the last addition, each data element in the collection are only a successor

Linear structure comprising : a linear table, stack, queue, strings, arrays, generalized table

Linear table is a finite sequence of n data elements.
basic concepts:
Here Insert Picture Description

2.2 Case introduced

2.1 Case univariate polynomial arithmetic
Pn (x) = p0 + p1x + p2x2 + ... + pnxn

The coefficients of the n + 1, n + 1 abstraction is an ordered sequence of elements with a linear table, said index number is implicit in the coefficients, i.e., the first Table 1-n + 1 respectively correspond x0-xn items coefficient.

2.2 Case computing sparse polynomial
S (x) = 1 + 3x10000
+ 2x20000 nonzero coefficient and index entries abstract linear table, each element of the table (coefficient index).
Case 2.3 library information management system
to book table abstract linear table, book information is the data element.

Linear type definition table 2.3

//抽象数据类型线性表的定义
ADT List{
	数据对象:D={ai|ai∈ElemSet, i=1,2,,n, n≥0}
	数据关系:R={<ai-1, ai >| ai-1, ai ∈D, i=2,,n}
	基本操作:
		InitList(&L)	//初始化结构
		  操作结果:构造一个空的线性表L。
		DestroyList(&L)		//销毁结构
		  初始条件:线性表L已存在。
		  操作结果:销毁线性表L。
		ListEmpty(L)	//判断线性表是否为空
  		  初始条件:线性表L已存在。
  		  操作结果:若L为空表,则返回TRUE,否则返回FALSE。
	    ListLength(L)	//求线性表中数据元素个数
 		  初始条件:线性表L已存在。
 		  操作结果:返回L中数据元素的个数。
		GetElem(L, i, &e)	//求线性表中指定位置元素
  		  初始条件:线性表L已存在, 1≤i ≤ ListLength(L) 。
	      操作结果:用e返回L中第i个数据元素的值。
	    LocateElem(L, e, compare())	//判断指定元素在线性表中的位序
		  初始条件:线性表L已存在,compare()是数据元素判定函数。
 		  操作结果:返回L中第1个与e满足compare( )的数据元素的位序。若这样的元素不存在,则返回值为0PriorElem(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无定义。
		ListTraverse(L, visit( ))	//遍历线性表
  	   	  初始条件:线性表L已存在。
  		  操作结果:依次对L中的每个数据元素调用函数visit( )。一旦visit( )失败,则操作失败。
		/* 以上除了初始化和销毁操作其他都是引用型操作 */

		ClearList(&L) 	//清空线性表
		  初始条件:线性表L已存在。
 		  操作结果:将L重置为空表。
		ListInsert(&L, i, e)	//在线性表的指定位置前插入新的元素
	      初始条件:线性表L已存在,1≤i≤Listlength(L)+1。
          操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1ListDelete(&L, i, &e)	//删除线性表指定位置上的元素
  		  初始条件:线性表L已存在且非空,1≤i≤Listlength(L)。
		  操作结果:删除L中第i个数据元素,并用e返回其值,L的长度减1/* 以上是加工型操作 */
		  
}ADT  List

Table 2.4 indicates a linear sequence and achieve

2.4.1 Linear sequence table stores a

顺序表的存储结构
#define  MAXSIZE	100 //顺序表的最大容量
typedef  struct{
	ElemType  *elem;	//顺序表在内存的基址
	            int 	length;	//当前顺序表的长度,即表中存放元素的个数
}SqList;	//顺序表的结构类型名为SqList

★ 2.4.2 order to achieve the basic operation of the table

1. 初始化:创建一个空的顺序表
Status  InitList(SqList &L) {	//构造一个空的顺序表L
	L.elem=new ElemType[MAXSIZE];
	if(!L.elem) exit(OVERFLOW);	//分配失败
	L.length=0;	//空表长度为0
	return OK;
} //InitList

Initialization: Create an empty sequence table

2. 取值:获取表中第i个数据元素的值
Status GetElem(SqList L, int i, ElemType &e){
 //若i<1或i大于表长,返回ERROR;否则用e返回第i个元素的值,并返回OK
 if(i<1||i>L.length) return ERROR; //i值不合法,返回ERROR
 e=L.elem[i-1]; //将第i个元素值赋给e
 return OK;
}//GetElem

基本操作:元素的赋值
赋值次数:1
时间复杂度:O(1)

Value: Get value table data in the i-th element

*修改:将表中第i个数据元素的值改为指定值
status ModifyElem(SqList &L, int i, ElemType e){
 //在顺序表L中修改第i个元素,且1≤i≤ListLength_Sq(L)
 if(i<1||i>L.length) return ERROR; //i值不合法,返回ERROR
 *(L.elem+i-1) =e; //将指定元素值赋给第i个元素
 return OK;
}//ModifyElem

基本操作:元素的赋值
赋值次数:1
时间复杂度:O(1)

Review: The i-th value table data element to a specified value

3. 查找:查找指定元素e
int LocateElem(SqList L, ElemType e){
 //在顺序表L中查找第1个值与e相等的元素的位序,若找到,返回其在L中的位序,否则返回0。
	for(i=0;i<L.length;i++) if(e==L.elem[i])  return i+1;
	return 0;
} //LocateElem

基本操作:元素的比较
最坏情况下比较的次数:n
平均比较次数:(n+1)/2
时间复杂度:O(n)

查找:查找指定元素e

4. 插入:将指定元素e插入到指定位置
   操作特点:插入位置后面的元素均需前后移一个位置
   合法的插入位置:1≤i≤ListLength(L)+1
   Status  ListInsert ( SqList &L, int i, ElemType e) {
//在顺序表L的第i个位置插入新的数据元素e,且1≤i≤ListLength(L)+1
	if(i<1||i>L.length+1)  return  ERROR;
	if(L.length==MAXSIZE)  return  ERROR;    //当前存储空间已满
	for(j=L.length-1; j>=i-1; j--)  
		L.elem[j+1]=L.elem[j];   //插入位置及其后的元素后移
	L.elem[i-1]=e;	//插入元素e
	L.length++//表长加1
	return OK;
} //ListInsert

基本操作:元素的移动
最坏情况下移动的次数:n
平均移动次数:2/n
时间复杂度:O(n)

 插入:将指定元素e插入到指定位置

5. 删除:将指定位置元素e删除
   操作特点:插入位置后面的元素均需前移一个位置
   合法的删除位置:1≤i≤ListLength(L)
Status  ListDelete(SqList &L, int i) {
//在顺序表L中删除第i个元素并用e返回其值,且1≤i≤ListLength_Sq(L)
	if(i<1||i>L.length)  return ERROR; 	//删除位置不合法
	for(j=i; j<=L.length-1; j++)  
		L.elem[j-1]=L.elem[j];  //被删除元素之后的元素向前移一个位置
	--L.length; 	//表长减1
	return OK;
} //ListDelete

基本操作:元素的移动
最坏情况下的移动次数:n-1
平均移动次数:(n-1)/2
时间复杂度:O(n)

删除:将指定位置元素e删除

Released eight original articles · won praise 49 · views 7030

Guess you like

Origin blog.csdn.net/weixin_43548815/article/details/104615026