20.11 Answers to Chapter Questions

I l @ ve RuBoard

Answer 20-1: The problem is with the statement:

 while ((current_ptr->data != value) &&         (current_ptr != NULL)) 

current_ptr->data is checked before we check to see whether current_ptr is a valid pointer (!= NULL). If it is NULL, we can easily check a random memory location that could contain anything. The solution is to check current_ptr before checking what it is pointing to:

 while (current_ptr != NULL) {      if (current_ptr->data == value)          break; 

Answer 20-2: The problem was as follows : because the first word in the dictionary was the smallest, every other word used the right-hand link. In fact, because the entire list was ordered, only the right-hand link was used. Although this was defined as a tree structure, the result was a linked list. See Figure 20-16.

Figure 20-16. Dictionary tree
figs/c++2_2016.gif

Some of the more advanced books on data structures, such as Wirth's Algorithms + Data Structures = Programs , discuss ways of preventing this by balancing a binary tree.

Trivia Answer: You give up. That's right, the 21st move is to resign.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net