Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Page 1212 - exercise #2 Suppose that stack is an object of type stackType<int>.

ID: 3553763 • Letter: P

Question

Page 1212 - exercise #2
Suppose that stack is an object of type stackType<int>. What is the difference between stack.top and stack.top - 1?

Page 1215 - exercise #12
Explain why, in the linked implementation of a stack, it is not necessary to implement the operation to determine whether the stack is full.

Page 1217 - exercise #18
What does the following function do?
void mystery(queueType<int>& q)
{
      stackType<int> s;
      while (!q.isEmptyQueue() )
      {
            s.push(q.front() );
            q.deleteQueue();
      }
     while (!s.isEmptyStack() )
     {
            q.addQueue(2 * s.top() );
            s.pop();
     }
}

Page 1218 - exercise #20
Suppose that queue is a queueType object and the size of the array implementing queue is 100. Also, suppose that the value of queueFront is 99 and the value of queueRear is 25.
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Page 1218 - exercise #22
Suppose that queue is a queueType object and the size of the array implementing queue is 100. Also, suppose that the value of queueFront is 99 and the value of queueRear is 99.
a. What are the values of queueFront and queueRear after adding an element to queue?
b. What are the values of queueFront and queueRear after removing an element from queue?

Explanation / Answer

Page 1217 - exercise #18:

The mystery function accepts a queue object as a parameter, doubles the every element in the queue, and stores the outcome elements in queue in the reverse order by using a stack object.

// mystery function

void mystery(queueType<int>& q)

{

          stackType<int> s;

The above statement creates a stack object s for stackType class of type int.

The conditional statement in the while loop verifies whether the queue q is empty or not. This loop repeats until the queue q is empty.

          while(!q.isEmptyQueue())

          {

This statement gets the front element from the queue q and inserts into the stack s.

               s.push(q.front());

This statement deletes the front element from the queue q.

               q.deleteQueue();

          } // end while

The conditional statement in the while loop verifies whether the stack s is empty or not. This loop repeats until the stack s is empty.

          while(!s.isEmptyStack())

          {

This statement gets the top element from the sack s, multiplies that element with 2, and inserts the result into the queue q.

q.addQueue(2 * s.top());

This statement deletes the top element from the sack s.

               s.pop();

          } // end while

} // end of mystery function

Hence the function doubles the every element in the queue, and stores the outcome elements in queue in the reverse order by using a stack object.

Sample output:

Before calling the mystery function, the elements in the queue are: 1 2 3 4 5

After calling the mystery function, the elements in the queue are: 10 8 6 4 2

Page 1218 - exercise #22:

An object of queueType class is queue. The maximum size of the array to store the elements into the queue is 100. The value of the queueFront is 99. The value of the queueRear is 99. So the queue is not full. And the queue is a circular queue in the queueType class.

a)      When inserting a new element in the circular queue, then the queueRear position will move to its next position that is 100. But here the maximum index is 99. So the queueRear position will automatically goes to the first position of the array (that is 0). The new element will be inserted into the queue at queueRear position. There will be no changes in the queueFront position.

Thus the value of the queueFront is 99. The value of the queueRear is 0.

b) When removing an element from the circular queue, the element at the position queueFront will be removed and the position queueFront will move to its next position that is 100. But here the maximum index is 99. So the queueFront position will automatically goes to the first position of the array (that is 0). There will be no changes in the position queueRear.

Thus the value of the queueFront is 0. The value of the queueRear is 99.