CSCI 131, Fall 2002, Test 4.
- The weight of each question is given with the question
- Use as much paper as you wish, but make your answers are legible.
- Please answer each question thoughtfully and carefully.
- [4 pts] Describe the difference between direct memory access and
indirect memory access (Pointers vs regular variables).
- [1 pt] A linked list is a data structure where each node contains
a data item of type ItemType and a pointer to the next item
in the list. Give a C++ type declaration for a node in such a
list.
- [3 pts] Write a procedure head(n,item) which given n, a pointer
to the next node in the list and item an ItemType to be inserted
into the data field, creates an instance of the node type described
above, fills in all fields,
and returns a pointer to the new node.
- [2 pts] A binary tree is a data structure where each node contains
a data item of ItemType, a pointer to a left child and a pointer
to a right child. Give the C++ type declaration for a node is such a
tree.
- [5 pts] Write a procedure root_it(l,r,item), which given l,
a pointer to a node to be the left child,
r a pointer to node to be the right child, and
item, an ItemType to be the data field, creates an instance of the
node type described above, fills in all of the fields,
and returns a pointer to the new node.
- [2 pts] What is a recursive definition?
- [3 pts] What conditions are necessary to write a recursive routine?
- [5 pts] Describe a recursive routine which will determine if an
array of characters contains a palindrome. A palindrome is a
word which reads the same forwards and backwards. You do not need
to give C++ code, pseudo code will do.
- [5 pts] Write a recursive routine which will calculate n!.
(n! = n*(n-1)! )
- [3 pts] Use trace the execution of your routine given
above for the values n=0, n=1 and n=4.
- [5 pts] Write a routine which given a pointer to a linked list
(as described in questions 2 and 3) and a character, prints
the list. If the character is F, the list is printed in order,
if the character is R, the list is printed in reverse order, and
if the character if B, the list is printed as a palindrome (forward
and then backwards).
-
[2 pts] Discuss the difference between using a loop and using recursion
to solve a problem.