Test 1 Fall 97
Test 1 Fall 97
Test 1 Fall 97
Owen Astrachan
October 8, 1997
Name:
Honor code acknowledgment signature
Problem 1
value grade
9 pts.
Problem 2 12 pts.
Problem 3 14 pts.
Problem 4 10 pts.
Problem 5 10 pts.
Problem 6 10 pts.
TOTAL:
65 pts.
This test has 9 pages, be sure your test has them all. Do NOT spend too much time on one question |
remember that this class lasts 50 minutes.
In writing code you do not need to worry about specifying the proper include header les. Assume that
all the header les we've discussed are included in any code you write.
Class declarations for stacks and queues are provided on the last page, you might want to tear this page o .
PROBLEM 1 :
Short-
and post-stu
9 points
part A 6 points
Evaluate the following post x expressions, you can show work for partial credit.
3 5 6 * + 8 +
7 8 2 - * 1 * 6
part B 3 points
In a sentence or two, what's the advantage of using the function Vector::append in client programs rather
than resizing the vector, e.g., why use
Vector string v;
string s;
while cin
s
v.appends;
instead of
Vector string v;
int count;
string s;
while cin
s
if count = v.length v.resizev.length*2;
v count = s;
count++;
PROBLEM 2 :
Reversal
of Fortune
12 points
Write the function reverse whose header is given below. The function reverse reverses the elements of the
queue q. For example, if q is represented by a,b,c,d, with a the rst element and d the last element of the
queue, then after the call reverseq q is represented by d,c,b,a. In part A you may de ne variables that
are stacks, queues, and ints, but not vectors. In part B you may only de ne variables that are queues or
ints.
part A 6 points You may de ne stack, queue, and int variables no vectors.
void reverseQueue int & q
pre: q represented by a1, a2, ..., an
post: q represented by an, ..., a2, a1, i.e., q is reversed
ne queue and int variables, but no stack variables hint: think recursively.
PROBLEM 3 :
part A 6 points
Consider the function blurb below. What is the exact value of the function calls blurb1 and blurb1024?
Using big-Oh notation, determine the running time and the value of blurbn? The running time is how
long the function takes to execute, as a function of n ; the value is what blurbn returns, again as a function
of n.
int blurbint n
int count=0;
while n
0
count++;
n = n
2;
return count;
n; k++
sum++;
Using big-Oh, what is the running time of the two loops as a function of n ? Brie y justify.
If k++ is replaced with k
Brie y justify.
*= 2,
what is the running time of the two loops using big-Oh, as a function of n ?
j; k *= 2
What is the big-Oh running time of the loops? Justify your answer
4
part C 4 points
Consider the function search below that performs a binary search correctly on a doubly linked list given
pointers to the rst and last nodes of the list. Write a recurrence relation for the function in terms of T n
where n is the number of elements in the list between and including rst and last. note: only one recursive
call is made for each invocation of search.
Using the substitution plug-in method, solve the recurrence. You may nd it helpful to note that
1 2 + 1 4 + 1 8 + ... = 1
PROBLEM 4 :
Moving
experiences
10 points
Write the function nd whose header is given below. nd returns a pointer to the rst node in list containing
key and returns NULL 0 if key does not appear in list. Note that list has a header node.
Node * findNode * list, const string & key
pre: list has a header node
post: return pointer to first node in list containing key
return NULL 0 if key is not in list
PROBLEM 5 :
Write the function VectorToList that creates a linked list, with a header node, whose nodes contain values
from the vector in the same order as the values in the vector. For example
vector<string> a
"bear"
"coyote"
"jackal"
"wolf"
"fox"
"jackal"
"coyote"
"wolf"
"fox"
PROBLEM 6 :
Garbage
Removal
10 points
Write a function that removes all nodes containing key from a doubly-linked, circular list, leaving the list
circular. In a one-node circular list the node's prev and next elds both point to the node itself.
void RemoveNode * & list, const string & key
pre: list is circular
post: all nodes with info value == key are removed, list is circular