攻克C语言链表.ppt

上传人:本田雅阁 文档编号:2462415 上传时间:2019-03-31 格式:PPT 页数:51 大小:754.01KB
返回 下载 相关 举报
攻克C语言链表.ppt_第1页
第1页 / 共51页
攻克C语言链表.ppt_第2页
第2页 / 共51页
攻克C语言链表.ppt_第3页
第3页 / 共51页
亲,该文档总共51页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《攻克C语言链表.ppt》由会员分享,可在线阅读,更多相关《攻克C语言链表.ppt(51页珍藏版)》请在三一文库上搜索。

1、Data Structure Using C,信息管理学院 尹晓 yx_,Chapter 5 Linked Lists,在本章中,我们将学到: 认识链表的特性 链表的三种类型 单链表 双链表 循环链表 稀疏矩阵 利用链表对多项表达式进行加法计算,假设有一个单链表中存储了100000个数字,这些数字从第一个节点开始依次按升序排序。 如果我们想以升序的方式显示这些数字,该怎么办呢? 答案是:从第一个节点开始遍历列表。,假如我们又需要以降序的方式显示这些数字,如何解决此问题呢? 单链表中每一个节点链接到序列中的下一个节点。这意味着我们只能以单向遍历列表。 要以降序的方式显示数字,我们需要反转(rev

2、erse)这个链表。,链接列表,1,100000,2,Header,3,.,反转单链表 ptr1=Header-link ptr2=ptr1-link ptr3=ptr2-link ptr1-link=NULL while ptr3不为空 ptr2-link=ptr1 ptr1=ptr2 ptr2=ptr3 ptr3=ptr3-link ptr2-link=ptr1 Header-link=ptr2,5,7,10,22,Header,10,10,15,反转结束,上述算法的存在什么问题? 无论你什么时候访问下一节点,你都需要调整三个变量的所有链接。 此方法的缺点:对长链表来说效率低且耗时。 如何

3、解决此问题? 如果列表中每一个节点不仅包含序列中其下一个节点的地址,而且还包含其前一节点的地址,那么此问题就可以解决。,链接列表,考虑下面的单链表。 我们可以在单链表的每个节点中引入一个额外的字段,它存储前一个节点的地址。这种类型的链表就是双链表 (doubly linked list)。,链接列表,Header,15,19,21,23,25,32,Header,15,19,21,23,25,32,Doubly linked lists are also called two-way list or two-way chain)。 在双链表中,每个节点需要存储: 数据 下一个节点的地址 前一个

4、节点的地址 定义双链表中节点的结构体类型 struct node int data; struct node *Llink; struct node *Rlink; ,链接列表(续),5.5 Doubly Linked List,A doubly linked list is a linear data structure in which each node can have any number of data fields and two link fields which are used to point to the previous node and the next node.

5、 (page 184),链接列表(续),5.5.1 Definition,The operations that can be performed on the doubly linked list are: a) insertion b) deletion c) search d) copy e) merge f) traverse The only difference between a singly linked list and doubly linked list is that two pointer nodes have to be modified. (page 184),链

6、接列表(续),5.5.2 Opertations on a Doubly Linked List,25,10,15,create a new node, the address is New. ptr=Header-Rlink if New is not NULL New-Llink=Header Header-Rlink=New New-Rlink=ptr ptr-Llink=New New-data=X,Header,在前端插入元素75,插入完成,75,a) Insertion operation i) at the front ii) at the end iii) at any pos

7、tion,75,25,10,15,在双链表的末尾插入一个节点 ptr=Header-Rlink while ptr-Rlink is not NULL ptr=ptr-Rlink create a new node, the address is New if New is not NULL New-Llink=ptr ptr-Rlink=New New-Rlink=NULL New-data=X else print “create fail”,Header,在末尾插入元素75,插入完成,a) Insertion operation i) at the front ii) at the en

8、d iii) at any postion,75,25,10,15,ptr=Header-Rlink while ptr-data !=X and ptr is not NULL ptr=ptr-Rlink create a new node, the address is New if ptr is NULL print “cant find X” return if ptr-data = X ptr1=ptr-Rlink New-Llink=ptr New-Rlink=ptr1 ptr-Rlink=New ptr1-Llink=New New-data=X,Header,在元素10后插入元

9、素75,插入完成,a) Insertion operation i) at the front ii) at the end iii) at any postion,25,10,ptr=Header-Rlink if ptr is NULL print “list empty” return else ptr1=ptr-Rlink Header-Rlink=ptr1 ptr1-Llink=Header free(ptr),Header,在前端删除元素,删除完成,b) Deletion operation: i) at the front ii) at the end iii) at any p

10、ostion,10,ptr=Header-Rlink if ptr is NULL print “list empty” return while ptr-Rlink is not NULL ptr=ptr-Rlink here, ptr points to the last node ptr1=ptr-Llink ptr1-Rlink=NULL free(ptr),Header,在末尾删除元素,删除完成,b) Deletion operation: i) at the front ii) at the end iii) at any postion,ptr=Header-Rlink if p

11、tr is NULL print “list empty” return while ptr-data!=X and ptr is not NULL ptr=ptr-Rlink if ptr is NULL print“cant find X” return if ptr-data=X ptr1=ptr-Llink ptr2=ptr-Rlink ptr1-Rlink=ptr2 ptr2-Llink=ptr1 free(ptr),Header,删除元素10,删除完成,15,25,b) Deletion operation: i) at the front ii) at the end iii)

12、at any postion,c) search operation in a doubly linked list,ptr=Header-Rlink while ptr-data!=X and ptr is not NULL ptr=ptr-Rlink if ptr is NULL print “cant find X” return if ptr-data=X return ptr,return ptr,查找成功,查找元素10,15,25,Header,d) copy operation,ptr1=Header-Rlink create a new node, the address is

13、 Header1 ptr2=Header1 ptr2-data=NULL ptr2-Llink=NULL while ptr1 is not NULL create a new node,the address is New New-data=ptr1-data ptr2-Rlink=New New-Llink=ptr2 ptr1=ptr1-Rlink ptr2=New ptr2-Rlink=NULL,复制结束,15,25,10,Header,New,15,New,10,New,25,e) merge operation in a doubly linked list,ptr=Header1-

14、Rlink while ptr-Rlink is not NULL ptr=ptr-Rlink ptr1=Header2-Rlink ptr1-Llink=ptr ptr-Rlink=ptr1 free(Header2),合并结束,15,25,10,Header1,27,35,18,f) traversing the doubly linked list,ptr=Header1-Rlink while ptr is not NULL print ptr-data ptr=ptr-Rlink,15,25,10,Header1,15,10,25,遍历结束,假定你正在开发一款动作游戏,其中会给每个游

15、戏者一套武器。 在屏幕上依次显示每种武器。一旦显示第n个武器后,就会再次显示第一次出现的武器,并且这种顺序会跟前面一样继续。 你将使用哪种数据结构来存储武器的列表?,我们可以使用单链表。 但是,武器需要以循环重复的次序显示。 因此,一旦显示了所有武器,指针必须从列表中第一个武器重新开始。 这需要在每次到达列表结尾时重新初始化指针。 在此情况下,如果遍历最后一个武器对应的节点后指针能够自动移到列表中的第一个武器,那将会更加简单。 使用循环链表(circular linked list)可以实现这一点。,A circular linked list is a linked list where t

16、he last node points to the header node. (page 198) In circlular linked list, there are no NULL links. Circular linked lists can be either of the two types: singly linked circular list doubly linked circular list,链接列表(续),5.6 Circular Linked List,15,9,11,23,5,12,15,9,11,23,5,12,5.6.2 singly linked cir

17、cular list,5.6.3 doubly linked circular list,There is a disadvantage that if there is no care in processing the data elements in the nodes, an infinite loop can occur.(page 199),To avoid this problem, a special node called a header node can be maintained without the data element.,The operations that

18、 can be performed on circular linked list are: a) insertion b) deletion c) traversing d) searching e) merging f) splitting g) copying,链接列表(续),5.6.4 Opertations on a Circular Linked List,traversing the circular linked list,ptr=Header-link while ptr != Header print ptr-data ptr=ptr-link,15,10,25,10,He

19、ader,15,10,25,遍历结束,insert a node in a circular linked list at the end,Create a new node, the address is New ptr=Header-link while ptr-link!=Header ptr=ptr-link if New is not NULL ptr-link=New New-link=Header New-data=X,15,10,25,10,Header,在末尾插入元素75,75,插入完成,Sparse Matrices(稀疏矩阵) is a matrices contiani

20、ng very few non-zero elements. (page 210) Representing large sparse matrices wastes huge amounts of memory spaces for storing zeros. There are two ways of representing sparse matrices: Array representation Linked-list representation,链接列表(续),5.7 Sparse Matrices,5.7.1 Array representation,Each non-zer

21、o element in the sparse matrix is represented the following form: (Row, Column, Value),0 1 2 3,0,1,2,3,4,5.7.2 Linked-List representation for Sparse Matrices,Each column is represented by a circularly linked list with a head node. Each row is also represented by a circularly linked list with a head

22、node. Each node in the structure other than a head node is a non-zero term of a matrix and is made up of 5 fields: (Row, Col, Down, Right, Value),链接列表(续),link to the next non-zero elements in the same column,link to the next non-zero elements in the same row,-8,6,7,-5,4,orthogonal list (十字链表),orthog

23、onal list (十字链表),第0行,第1行,第2行,第3行,第4行,第0列,第1列,第2列,第3列,orthogonal list (十字链表),-8,6,7,-5,4,第0列,第1列,第2列,第3列,第0行,第1行,第2行,第3行,第4行,orthogonal list (十字链表),-8,6,7,-5,4,第0列,第1列,第2列,第3列,第0行,第1行,第2行,第3行,第4行,orthogonal list (十字链表),-8,6,7,-5,4,第0列,第1列,第2列,第3列,第0行,第1行,第2行,第3行,第4行,Exercise:,0 1 0 0 0 0 -3 0 0 8 0 5

24、 0 0 0,Polynomial manipulation(计算多项式表达式) Dynamic storage management(动态存储管理) Garbage Collection(垃圾回收) Buddy Systems(伙伴系统),链接列表(续),5.8 Application,The general form of polynomial is: P(x)=anxn + an-1xn-1 +a1x+a0 在一个多项表达式中,每一项都伴有两个值:系数和指数 因此,若要用链表来表示一个多项表达式,每个节点应该包括以下信息: 系数(coefficient) 指数(exponent) 链接到

25、下一个节点的指针(the pointer to point the next node),链接列表(续),5.8.1 Polynomial manipulation,10,Coeff,Exp,coefficient,exponent,pointer to point the next node,Link,用单链表表示多项表达式: P(x) = 3x8 - 7x6 + 14x3 + 12x 5 注意:系数为 0的项不需要存储到节点中。 下面我们考虑加法操作: 加法(Addition of two polynomials),链接列表(续),i) Polynomial addition,Polyno

26、mial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,To add two polynomial, say P and Q. There are may be 3 cases: case 1: P.exp=Q.exp The coefficients of two nodes have to be added R.coeff = P.coeff + Q.coeff R.exp = P.exp case 2: P.exp Q.exp Copy the coefficient term of P to the coeffi

27、cient term of R. R.coeff = P.Coeff R.exp = P.exp Case 3: P.exp Q.exp Copy the coefficient term of Q to the coefficient term of R. R.coeff = Q. Coeff R.exp = Q.exp,10,a,n1,Link,10,b,n2,Link,P=axn1,Q=bxn2,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,读取 Polynomial 1 的第一个节点和 Po

28、lynomial 2 的第一个节点。 比较两个节点的指数值 。 从Polynomial 2 读取的节点的指数值较大(6 5)。 将指数值为6的节点添加到 Polynomial 3 的列表 。,10,9,6,Polynomial 3: 9x6,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,从Polynomial 2读取下一个节点。 比较Polynomial 1的当前节点的指数和Polynomial 2的指数值。 从Polynomial 1读取的节点的指数值较大 (5 4)。因此,将指数值为5的节点添加

29、到 Polynomial 3 的列表 。,10,4,5,Polynomial 3: 9x6 + 4x5,10,9,6,Polynomial 3: 9x6,从Polynomial 1读取下一个节点。 比较 Polynomial 1 和 Polynomial 2 的当前节点的指数值。这两个指数值相等, 都是4。 相加两个节点的系数。结果节点具有的系数值是 11(6 + 5)。 将结果节点添加到Polynomial 3的列表 。,10,11,4,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,10,4,5,

30、Polynomial 3: 9x6 + 4x5 + 11x4,10,9,6,Polynomial 3: 9x6 + 4x5,从两个列表读取下一个节点。 比较Polynomial 1和Polynomial 2 的当前节点的指数值。 从Polynomial 1 读取的节点指数值较大 (3 2)。 将指数值为3的节点添加到Polynomial 3的列表 。,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,10,11,4,10,4,5,Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3,

31、10,9,6,10,2,3,Polynomial 3: 9x6 + 4x5 + 11x4,从Polynomial 1读取下一个节点。 比较 Polynomial 1 和Polynomial 2 的当前节点的指数值。两个指数值相等,都是2。 将两个节点的系数相加。结果节点现在的系数值为6 (3 + 3)。 将结果节点添加到Polynomial 3的列表 。,10,6,2,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,10,11,4,10,4,5,Polynomial 3: 9x6 + 4x5 + 11

32、x4 + 2x3 + 6x2,10,9,6,10,2,3,Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3,Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x,Polynomial 2: 9x6 + 6x4 + 3x2,现在,Polynomial 2中没有节点了。 将第一个列表Polynomial 1剩下的节点添加到结果列表 。,10,7,1,10,6,2,10,11,4,10,4,5,Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2 + 7x,10,9,6,10,2,3,Polynomial 3: 9x

33、6 + 4x5 + 11x4 + 2x3 + 6x2,Pptr=PHeader-link Qptr=QHeader-link create a new node Rheader Rheader-coeff=NULL Rheader-exp=NULL Rheader-link=NULL Rptr=Rheader while (Pptr is not NULL) and (Qptr is not NULL) if Pptr-exp=Qptr-exp . else if Pptr-expQptr-exp . else .,1) assume that the two polynomials P an

34、d Q have already been created. 2) PHeader is the header of polynomial P 3) Qheader is the header of polynomial Q,create a new node New Rptr-link=New Rptr=New Rptr-coeff=Pptr-coeff+Qptr-coeff Rptr-exp=Pptr-exp Rptr-link=NULL Pptr=Pptr-link Qptr=Qptr.link,create a new node New Rptr-link=New Rptr=New R

35、ptr-coeff=Pptr-coeff Rptr-exp=Pptr-exp Rptr-link=NULL Pptr=Pptr-link,create a new node New Rptr-link=New Rptr=New Rptr-coeff=Qptr-coeff Rptr-exp=Qptr-exp Rptr-link=NULL Qptr=Qptr-link,6. while Pptr is not NULL create a new node New Rptr-link=new Rptr=new Rptr-coeff=Pptr-coeff Rptr-exp=Pptr-exp Rptr-

36、link=NULL Pptr=Pptr-link 7. while Qptr is not NULL create a new node New Rptr-link=new Rptr=new Rptr-coeff=Qptr-coeff Rptr-exp=Qptr-exp Rptr-link=NULL Qptr=Qptr-link,continue,课后习题,write a program to convert a singly linked list into a circularly linked list, and then, count the nodes of the circularly linked list.,Example:,15,10,25,10,Header,20,The length of the circular linked list is: 4,page 273 (10 and 11),本章要掌握的内容,1. 链表的特性。 2. 单链表的基本操作。 3. 双链表的基本操作。 4. 循环链表的基本操作。 5. 什么是稀疏矩阵?稀疏矩阵的两种压缩存储方法? 三元组表示法 十字链表表示法 6. 多项表达式加法操作的算法。,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1