Question: C++: Implement the stubbed out functions in the MyQueue class using two stacks. The function specifications are shown below. The MyQueue class has 2 stacks
C++:
Implement the stubbed out functions in the MyQueue class using two stacks. The function specifications are shown below. The MyQueue class has 2 stacks as instance variables, which represent the inner state of the queue. Therefore, if an object has been dequeued from the queue it should not be in either stack. If an object is enqueued into the queue it should be present in at least one stack.
Methods to implement (specs also in the .h file):
#include#include using namespace std;
templateclass MyQueue {
// these two stck are instance variables // by default, the access is private
stackfirst; stack second;
public:
// return the value of the oldest member
T front(){ // please implement this method
}
// add value val to MyQueue
void push(T val){ // please implement this method
}
// remove the oldest member from MyQueue
void pop(){ // please implement this method
} };
Use queueTest.cpp to test your implementation of the MyQueue class.
Part 2: MyStack
-------------------------------------------------
MyStack implementation
Implement the stubbed out functions in the MyStack class using two queues. The MyStack class has 2 queues as instance variables, which represent the inner state of the stack. So, like with MyQueue, if an item is pushed onto the stack, it should be in at least one queue, and if an object is popped, it should not be in either queue.
#include#include using namespace std;
templateclass MyStack {
// define two instance variables // by default, they are private queue
public:
// return the latest value of MyStack
T top(){
// please implement this method
}
// add value val to MyStack
void push(T val){ // please implement this method
}
// remove the oldest value from MyStack
void pop(){
// please implement this method
};
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
