Ans CH08
Ans CH08
Ans CH08
For Exercises 1-10, indicate which structure would be a more suitable choice for each of the following
applications by marking them as follows:
A. Stack
B. Queue
C. Tree
D. Binary search tree
E. Graph
1. A bank simulation of its teller operation to see how waiting times would
be affected by adding another teller.
B
2. A program to receive data that is to be saved and processed in the
reverse order.
A
3. An electronic address book ordered by name.
D
4. A word processor to have a PF key that causes the preceding command
to be redisplayed. Every time the PF key is pressed, the program is to
show the command that preceded the one currently displayed.
A
5. A dictionary of words used by a spelling checker to be built and
maintained.
D
6. A program to keep track of patients as they check into a medical clinic,
assigning patients to doctors on a first-come, first-served basis.
B
7. A program keeping track of where canned goods are located on a shelf.
A
8. A program to keep track of the soccer teams in a city tournament.
C
9. A program to keep track of family relationships.
C or E
10. A program to maintain the routes in an airline.
E
For Exercises 11 - 30, mark the answers true or false as follows:
A. True
B. False
The following algorithm (used for Exercises 31 - 33) is a count-controlled loop going from 1 through 5.
At each iteration, the loop counter is either printed or put on a stack depending on the result of Boolean
function RanFun(). (The behavior of RanFun() is immaterial.) At the end of the loop, the items on the
stack are popped and printed. Because of the logical properties of a stack, this algorithm cannot print certain
sequences of the values of the loop counter. You are given an output and asked if the algorithm could
generate the output. Respond as follows:
A. True
B. False
Set count to 0
WHILE (count < 5)
Set count to count + 1
IF (RanFun())
Write count, ' '
ELSE
Push(myStack, count)
WHILE (NOT IsEmpty(myStack))
Pop(myStack, number)
Write number, ' '
The following algorithm (used for Exercises 34 - 36) is a count-controlled loop going from 1 through 5.
At each iteration, the loop counter is either printed or put on a queue depending on the result of Boolean
function RanFun(). (The behavior of RanFun() is immaterial.) At the end of the loop, the items on the
queue are dequeued and printed. Because of the logical properties of a queue, this algorithm cannot print
certain sequences of the values of the loop counter. You are given an output and asked if the algorithm could
generate the output. Respond as follows:
A. True
B. False
C. Not enough information
Set count to 0
WHILE (count < 5)
Set count to count + 1
IF (RanFun())
Write count, ' '
ELSE
Enqueue(myQueue, count)
WHILE (NOT IsEmpty(myQueue))
Dequeue(myQueue, number)
Write number, ' '
insert tree like one on page 690 (11) of C++ Plus, 3rd edition. remove box and arrow as answer.
48. If Print is applied to the tree formed in Exercise 47, in which order
would the elements be printed?
Exercises 51 – 55 are short answer questions based on the following directed graph.
*******
insert graph from page 602 of C++ Plus, 4th edition
*******
51. Is there a path from Oregon to any other state in the graph?
No
52. Is there a path from Hawaii to every other state in the graph?
Yes
53. From which state(s) in the graph is there a path to Hawaii?
Texas
54. Show the table that represents this graph.
An F means that there in not a link; a T means there is a link.
Alaska F F F F T F F
California F F F F F F F
Hawaii T T F T F T F
New York F F F F F F F
Oregon F F F F F F F
Texas F F T F F F T
Vermont T T F T F F F
Alaska Calif. Hawaii New York Oregon Texas Vermont
Exercises 56 – 60 are short answer questions based on the following directed graph.
********
insert graph from page 719 of C++ Plus, second edition.
*******
61. Given the record List containing the array values and the variable
length, write the algorithm for GetLength.
GetLength
RETURN length
62. Assume that record List has an additional variable currentPosition,
initialized to the first item in the list. What is the value of
currentPosition?
0
63. Write the algorithm for MoreItems, which returns TRUE if there are
more items in the list, and FALSE otherwise.
MoreItems(list)
RETRUN List.currentPosition NOT equal to length
64. Write the algorithm for GetNext(myList, item) so that item is the next
item in the list. Be sure to update currentPosition.
GetNext(list, item)
Set item to list.values[list.currentPosition]
Set list.currentPosition to list.currentPosition + 1
65. Exercises 61- 64 create the algorithms that allow the user of a list to see
the items one at a time. Write the algorithm that uses these operations
to print the items in a list.
WHILE (MoreItems(list))
GetNext(list,item)