第二章线性表
考纲要求:
一、线性表的定义和基本操作
二、线性表的实现(自主命题中,大小题都有,可能会有2道以上算法设计题):
1.顺序存储
2.链式存储
3.线性表的应用
本章考点:
一、线性表的逻辑结构
二、顺序存储结构
三、链式存储结构
1.单链表
2.循环链表,双向链表
3.顺序表和链表的比较(具体问题时如何选择)
一、逻辑结构(题目涉及较少)
1.线性结构的基本特征:是一个数据元素的有序集(有次序)
1)存在唯一“第一元素”;
2)存在唯一“最后元素”;
3)除最后元素外均有“唯一后继”;
4)除第一元素外均有“唯一前驱”;
2.抽象数据类型的线性表定义如下:
ADT list
{
数据对象:
D={Ai|}
数据关系:
R1={Ai-1,Ai}
{设线性表为(a1a2a3an称之为a在表中的位置)}
数据操作:
结构初始化操作/new
结构销毁操作/delete
引用型操作/use/ via address use
加工型操作/change data that was in i position
}ADT list
3.具体操作
1)初始化操作
Initlist(&L) crate a null line list;
2)销毁操作
DestroyList(&L)
{ If (!null List) destroy list; }
3)引用操作
a.ListEmpty(L) have elements?
b.ListLength(L) how much elements?
c.PriorElem(L,cur_e,&pre_e) before element;
d.NextElem(L,cur_e,&next_e) next element;
e.GetElem(L,i,&e) get position i element;
f.LocateElem(L,e,compare()) current elements same with e;
g.ListTraverse(L,visit() ) visit every element of list L just once,
which with some stable rule;
4)加工操作
a.ClearList(&L) set list L with null;
b.PutList(&L,I,&e) put position i element;
c.listInsert(&L,I,e)insert element e to position i of list L;
d.Listdelete(&L,I,&e)delete element was in position i of list L;
4.可以利用上述基本操作的组合实现更复杂的操作。
For Instance 0:
Assume that: there have two sets A and B,called LA and LB,and now we need a new set A = AUB.
Transform :
find different LA with LB elements and insert into LA;
Analysis:
visit LA,LB, compare LA with LB,and insert into LA;
Operation:
a.Visit LB; getElem(L,B,I)-e;
b.Compare elements of LB with LA; locateElem(LA,e,compare());
c. if same,skip,else insert; ListInsert(LA,n+1,e)
For instance 1:
归并两个“其数据元素按值非递减有序排列”的有序表LA和LB,求得有序表LC也具有相同的特征;
Analysis:
a.Init LC null;
b.Get cur_e from LA and LB;
c.If Ai <= Bj insert Ai into LC, else insert Bj;
d.Until list over repeat step 23;
e.Other elements via rule insert;
Code :
Void margeList(list La,list Lb,list &Lc)
{
Initlist(Lc);
I = j = 1; k=0;
La_len = listlength(La);
Lb_len = listlength(Lb);
While(i <= La_len &&j<=Lb_len)
While(j<=Lb_len)
{
Getelem(La,i,ai);
Getelem(Lb,j,bj);
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);++i;}
}
While(j<=Lb_len)
{
Getelem(Lb,j,bj);
Listinsert(Lc,++k,bj);++j;
}
}//marge