首页 > 编程语言 > 详细

算法导论第三版10.2-4练习 - 修改链表查询方法

时间:2017-10-12 13:47:37      阅读:337      评论:0      收藏:0      [点我收藏+]

要求:

As written, each loop iteration in the LIST-SEARCH‘ procedure requires two tests:
one for x ≠ L.nil and one for x.key ≠ k. Show how to eliminate the test for
x ≠ L.nil in each iteration.

解法:

关键就是在while循环开始之前将哨兵sentinel的key值设为k,这样while循环无论链表中是否查询成功都会终止

伪代码

LIST-SEARCH‘(L, k)

1 L.nil.key = k

2 x = L.nil.next

3 while x.key ≠ k

4   x = x.next

5 if x == L.nil

6   L.nil.key = NIL

7 return x

算法导论第三版10.2-4练习 - 修改链表查询方法

原文:http://www.cnblogs.com/dgzhangning/p/7655663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!