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

Using C++ uisng microsoft visual studio For this computer assignment, you are to

ID: 3800574 • Letter: U

Question

Using C++ uisng microsoft visual studio For this computer assignment, you are to implement the Stack class using STL queues. In the implementation of the class, you are going to use queues q1 and q2 to store and manipulate data. You are suggested to keep all elements in one of the queues at anytime. More details are described below

#ifndef ASSIGNMNET4_H
#define ASSIGNMENT4_H
#include

class Stack {
private:
   std::queue q1, q2;
public:
   bool empty() const;
   int size() const;
   int top();
   void push(const int& val);
   void pop();
};

bool Stack::empty() const
{
   You need to make sure both q1 and q2 are empty
}

int Stack::size() const
{
   You need to count the number of elements in both q1 and q2.
}

int Stack::top()
{
   This method returns the newest element.If q1 is not empty, simply return the
   end element of q1.Otherwise q2 is not empty and simply return the end element of q2.
}

void Stack::push(const int& val)
{
   Simply add the element to a non - empty queue.If both queues are empty
   the new element can be added to an arbitrary queue.
}

void Stack::pop()
{
   This method removes the newest element.Since all elements are in one of
   the queues, say it is the source, you need to dump all elements except
   the newest to the other queue.And then remove the last(i.e.the newest) element in the source.

}

int main() {
    Stack s;
    string op;
    int val = 0;

   cout << "operation -- size front end" << endl;
   cin >> op;
   while ( !cin.eof() ) {
        if ( op == "push" ) {
            cin >> val;
            s.push( val );
            cout << op << " " << val << "    -- ";
        }
        else if ( op == "pop" ) {
            s.pop();
            cout << op << "       -- ";
        }
        else {
            cerr << "Error input: " << op << endl;
            return 1;
        }
        cout << setw(3) << s.size() << setw(5) << s.top() << endl;
        cin >> op;
    }

    while ( !s.empty() )
        s.pop();
    cout << "End -- size of Stack is: " << s.size() << endl;

    return 0;
}

output

operation -- size front end
push 1    --   1    1
push 2    --   2    2
push 3    --   3    3
pop       --   2    2
push 4    --   3    4
push 5    --   4    5
push 6    --   5    6
pop       --   4    5
push 7    --   5    7
push 8    --   6    8
End -- size of Stack is: 0

Explanation / Answer

1) bool Stack::empty() const
{
if(top<0) // Stack is empty when top<0

return true;
}

2)

int Stack::size() const
{
return(top);//top is index of stack it shows number of elements in s stack
}

3)

int Stack::top()
{
   This method returns the newest element.If q1 is not empty, simply return the
   end element of q1.Otherwise q2 is not empty and simply return the end element of q2.
}

4)

void Stack::push(const int& val)
{
if(top>maxsize)

print ("Stack is Full");

else

{ q1[top]=val;

q2[top]=val

top++;

}

}

5)

void Stack::pop()
{

temp=q1[top];

top--;
return(temp);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote