Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions