标签:search bsp 修改 算法导论 key值 each 循环 test loop
要求:
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
标签:search bsp 修改 算法导论 key值 each 循环 test loop
原文地址:http://www.cnblogs.com/dgzhangning/p/7655663.html