CS-215
Computer Science II
Spring 1998
Sample test 2 (100 points)
**** You will have approximately 6 problems on the second test
**** The test will cover "Stacks & Queues", "Recursion" and Section 17.1.
**************************************************************************
1. What do the initially empty stacks S and T "look like" after the
following sequence of operations:
stack S;
stack T;
S.push(1);
S.push(2);
T.push(3);
T.push(4);
int item1 = S.pop();
int item2 = T.on_top();
S.push(item2);
S.push(5);
int item3 = T.pop();
T.push(6);
2. (a) What is the corresponding postfix expression of the following
infix expression
3 - A / ( B * ( C + D ) )
(b) Use a stack to evaluate the following postfix expression (from left
to right). Show your work.
2 4 + 6 2 *
3. What do the initially empty queues Q and T "look like" after the
following sequence of operations:
queue Q;
queue T;
Q.enqueue(1);
Q.enqueue(2);
T.enqueue(3);
T.enqueue(4);
int item1 = Q.dequeue();
int item2 = T.at_front();
Q.enqueue(item2);
Q.enqueue(5);
int item3 = T.dequeue();
T.enqueue(6);
4. For each of the following situations, which ADT, stack or queue, would
be most appropriate?
a. The customers at a deli counter who take numbers to mark their turn.
b. The boxes in a box trace of a recursive function.
c. The items on a cash register tape.
d. A program that uses backtracking.
5. Suppose you have a circularly linked list (the last node points to the
first node of the list), with the HEAD pointer pointing to the last
node of the list, to represent a queue.
Assume that all you know about the queue is the HEAD pointer, i.e.,
there are no special pointers called FRONT and REAR as normally
associated with queues.
The following member functions of the queue class DELETE and INSERT
intend to delete and insert elements on the queue.
Can you tell if they are correctly designed? If they are, say so.
Otherwise, make necessary changes to make them correct.
class queue {
public:
...
void INSERT(const int &item):
boolean DELETE(int &item);
...
protected:
struct node ;
typedef node *node_ptr;
struct node {
int data ;
node_ptr next;
}
node_ptr HEAD ;
};
void queue :: INSERT(const int &item) {
node_ptr p;
p = get_node(item)
If (HEAD == NULL) {
HEAD = p ;
HEAD -> next = p;
}
else {
p -> next = HEAD -> next ;
HEAD -> next = p ;
}
}
boolean queue :: DELETE(int &item) {
node_ptr p ;
if (HEAD != NULL) {
if (HEAD == HEAD->next) {
item = HEAD->data;
p = HEAD;
HEAD = NULL;
delete p;
}
else {
p = HEAD->next;
item = p->data;
delete p;
}
else
return FALSE;
}
6. Given the C++ declarations:
class queue {
public:
...
void INSERT(const int &item):
boolean DELETE(int &item);
...
protected:
typedef int QueueArray[20];
QueueArray Queue;
int Front, Rear;
};
(1). If Queue is considered as a circular queue (with modulo n
arithmetic) then how would you detect if the queue is empty?
When is the circular queue considered full?
(2). Write a simple function to insert a new item x into the
circular queue Queue.
void INSERT(const int &item):
7. Given the C++ declarations:
class queue {
public:
...
void INSERT(const int &item):
boolean DELETE(int &item);
...
protected:
struct node;
typedef node *node_ptr;
struct node {
int data;
node_ptr next;
}
node_ptr FRONT, REAR;
};
(1). how would you initialize a queue using the pointer variables FRONT
and REAR? When is such a queue considered empty?
(2). Write a simple function to delete a node from the queue.
boolean DELETE(int &item);
8. What is a priority queue? How would you initialize a priority queue?
How do you detect if the segment corresponding to a particular priority
is empty? How do you delete a node of priority i from a priority queue?
You may use an auxiliary diagram to explain some of your points.
9. Write a recursive function called NumberEqual that receives as
parameters and integer array A and its size N, and an integer value
X. The function returns an integer count of the number of integers
in the array A that are equal to the integer X. For example, if A
contains the 10 integers 1, 2, 4, 4, 5, 6, 7, 8, 9, and 12, then
NumberEqual(A, 10, 4) returns the value 2 because 4 occurs twice in A.
10. The formula 2**N-1 is somewhat "famous" in mathematics. When
N = 3 2**N-1 is 7 (a prime number)
4 15 (non prime)
5 31 (a prime)
6 63 (non prime)
7 127 (a prime)
8 255 (non prime)
9 511 (a prime)
10 1023 (non prime)
Consider the question: Is 2**N-1 prime when N is an odd number?
Is this the type of question which can be answered by recursion?
Explain your answer.
11. 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... is called the Harmonic Series.
Write a recursive function to calculate the sum of the first n
terms in the Harmonic series.
12. What output does the following program produce?
#include
int Search(int First, int Last, int N)
{
int ReturnValue;
cout << "Enter: First = " << First << " Last = " << Last << endl;
int Mid = (First + Last)/2;
if ((Mid * Mid <= N) && (N < Mid+1) * (Mid+1))
ReturnValue = Mid;
else if (Mid * Mid > N)
ReturnValue = Search(First, Mid-1, N);
else
ReturnValue = Search(Mid+1, Last, N);
cout << "Leave: First = " << First << " Last = " << Last << endl;
return ReturnValue;
} //end Search
int Mystery(int N)
{
return Search(1, N, N);
} //end Mystery
int main()
{
cout << Mystery(30) << endl;
return 0;
} //end main
13. Consider the following function to compute N!
int Factorial(int n) {
int j, result;
result = 1;
for (j = 2; j <= n, j++)
result = result * j;
return result;
}
Is this recursive or non-recursive? Will it work?
Explain why or why not.
14. What are the preorder, inorder, and postorder traversals of the
following binary tree?
A
\
\
B
/
/
C
\
\
D
/
/
E
\
\
F
15. Convert the following infix expression to prefix. Sketch the binary
expression tree associated with the expression (you may want to convert
to prefix first!).
M+N*(X+Y*Z)-Q/R