Name ___________________
CPTR246 Spring '99 (100 total points)
Exam 2
Read the instructions to each question carefully and completely before
answering.
-
What is the output produced by the following code? (5 points)
int x(5);
int * a;
a = &x;
*a = *a + x;
cout << x << endl;
cout << *a << endl;
-
Below is a visual representation of an ordered linked list of integers
(i.e., a linked list of integers whose values are kept in order from
lowest to highest). Indicate the changes that would take place if the
following integers were added: 15, 38, 95. (10 points)
(link list would not display - use example given in class)
-
What do the acronyms (initials) FIFO and LIFO stand for? (5 points)
FIFO -
LIFO -
-
Suppose you are implementing a stack of integers as a linked list.
(10 points)
-
Draw the visual representation (as was done in question 2) of the stack
after the following stack operations are executed. Assume that the stack
is empty when you start!
push 5
push 8
push 12
pop
push 10
push 4
pop
-
What two integers were popped?
-
Suppose you are implementing a queue of integers as a linked list.
(10 points)
-
Draw the visual representation (as was done in question 2) of the queue
after the following queue operations are executed. Assume that the queue
is empty when you start!
enqueue 5
enqueue 8
enqueue 12
dequeue
enqueue 10
enqueue 4
dequeue
-
What two integers were dequeued?
-
From the Standard Library Handout, we know that the following functions
are available in C++.
(these functions are given in our current textbook on page 325)
Using the following object definitions and the given library functions,
write C++ code to accomplish the specified tasks.
char stringA[50];
char stringB[50];
char miniString[4];
-
Copy the first 5 characters in string stringA
to stringB. (Only
one statement is necessary). (5 points)
-
Display the message "Substring found"
if miniString is a
substring of stringA.
Otherwise, display "Substring not
found". (5 points)
-
What is a memory leak? (5 points)
-
I'm writing a class named CARD
and you're writing a class named DECK.
In my class declaration, I code
friend class DECK;
Explain the effect of this line of code. (5 points)
-
Define ADT (Abstract Data Type). (5 points)
-
You have received a copy of queue.h,
the class implementation for a queue. You are to add two new member
functions to the class, Size
and LineJumper. Each
is described below.
Size - this function
will compute and return the number of nodes (elements) in the queue. An
example of its use would be:
Queue myQueue; // define queue
object
. . . // code to load the queue
cout << myQueue.Size()
<< endl;
LineJumper - this
function inserts a new node (element) into the queue, but not in the normal
position as is done with the Enqueue
member function. This function takes as parameters the integer value to
be inserted and the position in the queue it is to take. For example, if
myQueue contains
6 5 9 2
and the following command is executed
myQueue.LineJumper(8, 3);
then the integer 8 is inserted into the queue and has the third position.
The queue would then have
6 5 8 9 2
You may assume that the position is a valid one for line jumping -- that
is if the queue currently has n nodes, then the position passed
to LineJumper will
be a number from 1 to n.
You are to complete the code for these functions on the following two
pages.
Question 10 (continued …)
For the Size
member function: (10 points)
Code the prototype that would be inserted into the public
section:
Code for the function definition:
Question 10 (continued …)
For the LineJumper
member function: (25 points)
Code the prototype that would be inserted into the public
section:
Code for the function definition:
Please give a 4-digit number, known only to you, that I can use for
posting grades: _____