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

#include <iostream> #include <string.h> #include <cstdlib> #include <ctime> #inc

ID: 3700591 • Letter: #

Question

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

const int MAX_SIZE = 200;     // Define maximum length of the queue

class Queue
{
    private:
        int head, tail;           // Index to top of the queue
        int theQueue[MAX_SIZE]; // The queue

    public:
        Queue();         // Class constructor
        bool Enqueue(int num);   // Enter an item in the queue
        int Dequeue();          // Remove an item from the queue
        bool isEmpty();          // Return true if queue is empty
        bool isFull();           // Return true if queue is full
        void displayQueue();
};
Queue::Queue()
{
   head = tail = -1;
}
bool Queue::Enqueue(int num)
{
   // Check to see if the Queue is full
   if(isFull()) return false;

   // Increment tail index
   tail++;
  
   if(tail == MAX_SIZE)
   {
       tail = 0;
   }
   // Add the item to the Queue
   theQueue[tail % MAX_SIZE] = num;
   return true;
}
int Queue::Dequeue()
{
   int num;

   // Check for empty Queue
   if(isEmpty()) return ''; // Return null character if queue is empty
   else
   {
       head++;
       num = theQueue[head % MAX_SIZE];       // Get character to return
       return num;               // Return popped character
   }
}

bool Queue::isEmpty()
{
   return (head == tail);
}
bool Queue::isFull()
{
   // Queue is full if tail has wrapped around
   //   to location of the head. See note in
   //   Enqueue() function.
   return ((tail - MAX_SIZE) == head);
}

void Queue::displayQueue()
{
        if(tail > head)
       {
            for (int i = head + 1; i <= tail; i++)
           {
                cout << setw(3) << left << theQueue[i];
                cout << " ... ";
            }
        }
       else
       {
            for (int i = head + 1; i < MAX_SIZE; i++)
           {
                cout << setw(3) << left << theQueue[i];
                cout << " ... ";
            }
            for (int i = 0; i <= tail; i++)
           {
                cout << setw(3) << left << theQueue[i];
                cout << " ... ";
            }
        }
    cout << endl << endl;
}

Given a Stack S;(Bonus 20 points) – only get bonus if you finish the first 2 programs

Write code to reverse all the elements in S. Algorithm: Create a temporary queue; transfer all of the stack’s elements into the queue; transfer all of the queue’s items back into the stack.

Note: The algorithm for this question is very similar to the example given above

Explanation / Answer

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int MAX_SIZE = 200;    
class Stack
{
    private:
        int top;          
        int theStack[MAX_SIZE];
    public:
        Stack();        
        bool push(int num);  
        int pop();         
        bool isEmpty();         
        bool isFull();          
        void displayStack();
};
Stack::Stack()
{
    top = -1;
}
bool Stack::push(int num)
{
   
    if(isFull()) return false;
    top++;
    theStack[top] = num;
    return true;
}
int Stack::pop()
{
    int num;

    if(isEmpty()) return -1;
    else
    {
        num = theStack[top];     
        return num;              
    }
}
bool Stack::isEmpty()
{
    return (top == -1);
}
bool Stack::isFull()
{
    return (top == MAX-1);
}
void Stack::displayStack()
{
    for (int i = 0; i<=top; i++){
        cout << theStack[i] << " ";
    }
    cout << endl << endl;
}

For a Given Stack S and Queue as given in the question
the code to reverse the elements in S

Stack S;
Queue Q
while (!S.isEmpty()){
    Q.Enqueu(S.pop());
}
while (!Q.isEnpty()){
    S.push(Q.Dequeue());
}