Written Exercise:
Chapter 7:
#13, 14, 15, 16, 18, 19, 21, 22, 23, & 24 on pages 377 to 381
Chapter 8:
#7, 8, 9, 10, 11, 12, 13, 15, 16, 17, & 18 on pages 426 to 429

Chapter 7
#13, 14, 15, 16, 18, 19, 21, 22, 23, & 24 on pages 377 to 381
13. What is the output from the following sequence of stack operations?
stack<int> intStack;
int x, y=3;

intStack.push(8);
intStack.push(9);
intStack.push(y);
x = intStack.top();
intStack.pop();
intStack.push(18);
x = intStack.top();
intStack.pop();
intStack.push(22);
while(!intStack.empty())
{
   y = intStack.top();
   intStack.pop();
   cout<<y<<" ";
}
cout<<x<<endl;

14. List the elements on the stack after each operation:
stack<int> intStack;
int i;

intStack.push(8);
intStack.push(5);
intStack.push(3);
intStack.push(intStack.top());
intStack.push(intStack.top() * 2);
i = intStack.top();
intStack.pop();
intStack.push(2*i);

15. Write a function
template <typename T>
void stackClear(stack<T>& s);
      that clears a stack s.  Why is it critical that s be passed by reference?

16. What is the action of the following code segment?

stack<T> s1, s2, tmp;
T x;
...
while(!s1.empty())
{
    x = s1.top();
    s1.pop();
    tmp.push(x);
}
//assume s2 is empty
while(!tmp.empty())
{
    x = tmp.top();
    tmp.pop();
    s1.push(x);
    s2.push(x);
}
18. The function f() uses a second stack to modify the contents of an initial stack s:
template <typename T>
void f(stack<T>& s, const T& item)
{
    stack<T> tmpStack;
    T tmpVal;
    bool have = true;

    while(!s.empty() && s.top() != item)
    {
        tmpVal = s.top();
        s.pop();
        tmpStack.push(tmpVal);
    }

    if (s.empty() == true)
        have = false;
    else
        s.pop();

    while(!tmpStack.empty())
    {
        s.push(tmpStack.top());
        tmpStack.pop();
    }

    if(have)
        s.push(item);
}

       Assume that s has the values
                      1  5  8  10  15  22  55
       where 55 is on top of the stack.  After executing
                f(s, 5);
       what is the output from the statements
while(!s.empty())
{
    cout<<s.top()<<" ";
    s.pop();
}
19. Trace the function f():
template <typename T>
void f(stack<T>& s)
{
    vector<T> v;
    int i;

    while(!s.empty())
    {
        v.push_back(s.top());
        s.pop();
    }
    for(i=0; i < v.size(); i++)
        s.push(v[i]);
}

Assume that s has the values 3  4  9  12  15  8  3  5, where 5 is on the top of the stack.
What is the output from the following statements?

f(s);
while(!s.empty())
{
    cout<<s.top()<<" ";
    s.pop();
}
21. Convert the following infix expressions to postfix:
        (a) a + b * c
        (b) (a + b) / (d - e)
        (c) (b^2 - 4 * a * c) / (2 * a)

23. Draw the sequence of stack configurations in the evaluation of the following postfix expression:

4  5  *  6  3  +  /  8  +
24. Draw the sequence of stack configurations and the developing output string in the conversion of
      the following infix expression to postfix:
7  *  (5  +  2  *  3)  -  2
Chapter 8
#7, 8, 9, 10, 11, 12, 13, 15, 16, 17, & 18 on pages 426 to 429
7. A queue is a structure implementing which of the following properties (circle all that apply)/
    (a)  first-in / last-out
    (b) last-in / first-out
    (c) first-come / first-serve
    (d) first-in / first-out
    (e) last-in / last-out

8. A queue is an applicable data structure for (circle all that apply)
    (a) expression evaluation
    (b) an operating-system job scheduler
    (c) simulation of waiting lines
    (d) printing a list in reverse order

9. What is the output from the following sequence of queue operations?

queue<int> q;
int x = 5, y = 3;

q.push(8);
q.push(9);
q.push(y);
x = q.front();
q.pop();
q.push(18);
x = q.front();
q.push(22);
while(!q.empty())
{
    y = q.front();
    q.pop();
    cout<<y<<" ";
}
cout<<x<<endl;

10. List the elements in the queue after each of the following operations:
queue<int> intQueue;

intQueue.push(18);
intQueue.push(2);
intQueue.push(intQueue.front());
intQueue.push(intQueue.front());
intQueue.pop();

11. What is the action of function f()?
template<typename T>
void f(queue<T>& q)
{
    stack<T> s;
    T elt;

    while(!q.empty())
    {
        elt = q.front();
        q.pop();
        s.push(elt);
    }

    while(!s.empty())
    {
        elt = s.top();
        s.pop();
        q.push(elt);
    }
}

   Why is it critical that q be a reference argument?

12. What is the action of the following code segment?

queue<int> q1, q2;
int x;
int n = 0, i;
  ...
//assume q2 is empty and n = 0
while (!q1.empty())
{
    x = q1.front();
    q1.pop();
    q2.push(x);
    n++;
}

for(i=0; i < n; i++)
{
    x = q2.front();
    q2.pop();
    q1.push(x);
    q2.push(x);
}

13. What is the action of function f( )?
template<typename T>
T f(const queue<T>& q)
{
    queue<T> qtmp;
    int i, qsize = q.size();

// assign q to qtmp
qtmp = q;

for(i=0; i<qsize-1; i++)
  qtmp.pop();

return qtmp.front();
}
15. Give the output of the following statements:
priority_queue<int> pq;
int arr[] ={10, 50, 30, 40, 60};
int i, x, y;

for(i=0; i<5; i++)
   pq.push(arr[i]);

pq.push(25);
x = pq.top();
pq.pop();
pq.push(35);
y = pq.top();
pq.pop();
cout<<x<<" "<<y<<endl;
while(!pq.empty())
{
    cout<<pq.top()<<" ";
    pq.pop();
}
cout<<endl;

16. Give the output of the following statements:
priority_queue<int> intPQ;  //empty priority queue

intPQ.push(5);
intPQ.push(27);
intPQ.push(25);
intPQ.pop();
intPQ.push(intPQ.top() * 2);
intPQ.push(intPQ.top() * 5);
while (!intPQ.empty())
{
    cout<< intPQ.top()<<" ";
    intPQ.pop();
}
cout <<endl;

17. Declare a stack, queue, and priority queue of integers, as follows:
stack<int> s;
queue<int> q;
priority_queue<int> pq;
      Assume that you input the integer sequence
          5  8  12  15  1  3  18  25  18  35  2  55
     and insert each value into each container in the order given.
     What is the output of the following statements?
while( !s.empty())
{
    cout<<setw(5)<<s.top()<<setw(5)
        <<q.front() <<setw(5)
        <<pq.top() <<endl;
    s.pop();
    q.pop();
    pq.pop();
}
18. Assume that a queue, intQueue, has the elements 5  1  3  9  8, from front to back.
      (a) Assume that you remove the elements from the queue and push them onto a stack.
           List the contents of the stack from top to bottom.
      (b) Assume that you remove the elements from the queue and push them onto a priority queue.
           Then remove the elements from the priority queue and push them onto a stack.
           List the contents of the stack from top to bottom.