Queues In this lab you will use 2 queues to simulate a stack. You are restricted
ID: 3570011 • Letter: Q
Question
Queues
In this lab you will use 2 queues to simulate a stack. You are restricted to ONLY use the following queue operations empty, front, pop and push.
template
class Stack
{
public:
Stack(); // create an empty stack
bool empty(); // returns true if the stack is empty
void push(T item);// adds item to the top of stack
void pop( );// removes an item from the stack
T top( );// returns at the top of the stack
private:
queue items; // holds the items you push (in reverse)
queue aux; // helpful for pop and top
}
1) Implement the Stack above using only the queue operations front, empty, pop, and push.
2) Create a driver program that pushes 1
Explanation / Answer
Answer:
1. Class queue
{
public:
front() // create an empty queue
bool empty(); // returns true if the queue is empty
void push(T item);// adds item into queue
void pop( ); // removes an item from the queue
T top( ); // returns at the top of the queue
private:
queue items; // holds the items you push
queue aux; // helpful for pop and top
}
2.
class Driver {
private:
std::vector<T> elems;
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
};
template <typename T>
void Stack<T>::push (T const& elem)
{
elems.push_back(elem);
}
template<typename T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("empty stack");
}
elems.pop_back(); }
template <typename T>
T Stack<T>::top () const
{
if (elems.empty()) {
throw std::out_of_range("empty stack");
}
return elems.back();
}
template<>
class Stack<std::string> {
private:
std::deque<std::string> elems;
public:
void push(std::string const&); // push element
void pop(); // pop element
std::string top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
};
void Stack<std::string>::push (std::string const& elem)
{
elems.push_back(elem);
}
void Stack<std::string>::pop ()
{
if (elems.empty()) {
throw std::out_of_range
("empty stack");
}
elems.pop_back(); // remove last element
}
std::string Stack<std::string>::top () const
{
if (elems.empty()) {
throw std::out_of_range
("empty stack");
}
return elems.back(); // return copy of last element
}
int main()
{
Stack<int> intStack; // stack of ints
Stack<std::string> stringStack; // stack of strings
// manipulate int stack
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.push(4);
intStack.push(5);
intStack.push(6);
intStack.push(7);
intStack.push(8);
intStack.push(9);
intStack.push(10);
std::cout << intStack.top() << std::endl;
intStack.pop();
// manipulate string stack
stringStack.push("cat");
stringStack.push("dog");
stringStack.push("frog");
stringStack.push("fish");
std::cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.