Name ___________________
CPTR246 Spring '01 (100 total points) Exam 3

push 35
push 65
push 8
push 18
pop
push 99
pop
enqueue 35
dequeue
enqueue 65
enqueue 8
dequeue
enqueue 18
enqueue 99

What is the root? (2 points)
How many nodes are in the tree? (2
points)
List the leaves in the tree. (2
points)
Name the children of node 8: (2
points)
Name the parent of node 8: (2 points)
List the nodes as they would be visited in a breadth-first
search: (3 points)
List the nodes as they would be visited in a depth-first
(preorder) search: (3 points)
Title of the book(a string
of at most 80 characters)
Name of the author (a string
of at most 40 characters)
Number of pages in the book
(an integer)
Genre of the book (a
characters object, like ‘M’ for mystery, etc.)
int
bookCount;
Write code that will display on the screen the title of all books in your
collection written by Mark Twain. (10
points)
TimesThere takes an integer as a
parameter and returns the number of times that integer appears on the
stack. Therefore, it will take 1 parameter, the integer to
be counted and returns an integer.
SwapTopTwo will do just that, reverse
the first two entries in the stack, by changing the links (pointers). It has no
parameters, but does return
false if there are less than two entries on the stack, true otherwise.
(a) Begin by indicating what
changes need to be made to the class declaration: (5 points)
class
Stack {
public: //
see function def's for pre and post
Stack() { top = 0;}
~Stack();
bool IsEmpty() {return (top == 0);}
bool IsFull();
bool Peek(int & x);
void Push(int x);
bool Pop(int & x);
void Display();
private:
Node * top; // points to the first Node in the stack
};
Use the following two pages
to code the functions.
(b) Write
the member function definition for TimesThere: (15 points)
(repeated for your convenience)
TimesThere takes an integer as a
parameter and returns the number of times that integer appears on the
stack. Therefore, it will take 1 parameter, the integer to
be counted and returns an integer.
(c) Write the member function definition for
SwapTopTwo: (15 points)
(repeated for your convenience)
SwapTopTwo will do just that, reverse
the first two entries in the stack, by changing the links (pointers). It has no
parameters, but does return
false if there are less than two entries on the stack, true otherwise.