Question: CSCI 340 Computer Assignment 4 Spring 2018 (10 points) For this computer assignment, you are to implement the Queue class using STL stacks. Queue.h contains
CSCI 340 Computer Assignment 4 Spring 2018
(10 points)
For this computer assignment, you are to implement the Queue class using STL stacks.
Queue.h contains the definition of the Queue class. It is given here to facilitate
the following description:
class Queue {
private:
std::stack
public:
bool empty() const;
int size() const;
int front();
int back();
void push(const int& val);
void pop();
};
You are required to implement this class in assignment4.cc. In this file, the main
function is already provided. The driver program works with an input file
assignment4input.txt.
In the implementation of the class, you are going to use stacks s1 and s2 to store and
manipulate data. You are suggested to use only s1 to save the element just pushed in
(i.e. enqueueed), and use s2 to hold older elements. More details are described below.
empty() : You need to make sure both s1 and s2 are empty.
size() : You need to count the number of elements in both s1 and s2.
front(): This method returns the oldest element. First of all if s2 is empty, move
all elements from s1 to s2. Simply return the top element in s2.
back(): This method returns the newest element. Simply return the top element
in s1.
push(): Simply add the element to s1.
pop(): This method removes the oldest element. You can reuse the method
front().
Use the following provided main, make sure to add your functions here:
#include
#include
#include "Queue.h"
using namespace std;
int main() {
Queue q;
string op;
int val = 0;
cout << "operation -- size front end" << endl;
cin >> op;
while ( !cin.eof() ) {
if ( op == "push" ) {
cin >> val;
q.push( val );
cout << op << " " << val << " -- ";
}
else if ( op == "pop" ) {
q.pop();
cout << op << " -- ";
}
else {
cerr << "Error input: " << op << endl;
return 1;
}
cout << setw(3) << q.size() << setw(5) << q.front() << setw(5) << q.back() << endl;
cin >> op;
}
while ( !q.empty() )
q.pop();
return 0;
}
Use Input txt:
push 1
push 2
push 3
push 4
pop
push 5
push 6
push 7
pop
push 8
push 9
Your output should look like this:
operation -- size front end
push 1 -- 1 1 1
push 2 -- 2 1 2
push 3 -- 3 1 3
push 4 -- 4 1 4
pop -- 3 2 4
push 5 -- 4 2 5
push 6 -- 5 2 6
push 7 -- 6 2 7
pop -- 5 3 7
push 8 -- 6 3 8
push 9 -- 7 3 9
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
