Lab 5: Queue from 2 Stacks (100 pts)
Due Tuesday, February 13 at 9:20am on Canvas


Pair Programming Extra Credit Opportunity (5 pts)

  • Both partners fill in, sign and date, and submit the pair programming contract.
  • Only ONE partner submits the lab assignment on Canvas. Please make sure both your names are on ALL FILES.

Your Objective
  • Here is a common interview question: implement a stack using two queues.
  • We will create an implementation of this interview question for Lab 5, and you will then be ready if you are asked this question on your next interview!
  • Given the below header file (must remain unchanged), write the Stack functions whose prototypes are provided in a file named Stack.cpp.
  • Test your functions inside a test file named StackTest.cpp
  • Name your header file Stack.h and your source file Stack.cpp
  • These files should be placed inside of a C++ project in Eclipse, along with your Queue.h from Lab 3.


Stack.h

  • Copy and paste the below code into a file named Stack.h.
  • You are not permitted to alter anything about this file.
  • Note: This file is not to be submitted.
    • The instructor will test your code using the below file.
  • Thus, make sure your source code compiles with the given header file.


#ifndef STACK_H_
#define STACK_H_


#include <iostream>
#include <string>
#include "Queue.h"
using namespace std;

class Stack
{
    public:
      
        /**manipulation procedures*/

        void pop();
        //removes an element from the top of the stack
        //precondition: the stack isn't empty
        //postcondition: an element has been removed from the top of the stack

        void push(string data);
        //adds an element to the top of the stack
        //postcondition: an element added to the top of the stack

        /**accessors*/

        string getTop();
        //returns the element at the top of the stack
        //precondition: the stack is not empty

        int getSize();
        //returns the size of the stack

        bool isEmpty();
        //returns whether the stack is empty

        /**additional queue operations*/
        void print();
        //prints the elements in the stack to stdout


    private:
        Queue<string> q1;
        Queue<string> q2;

};



#endif /* STACK_H_ */



What to Submit

  • Submit your Queue.h, Stack.cpp and StackTest.cpp files to Canvas when you are finished.
  • Please do not submit Stack.h or submit your files as a zip file.
  • -15 points for a missing test file
  • -14 points for each missing or incorrect function
  • -4 points per function for not handling the precondition such that it displays an appropriate error message (not mentioning the Queue)
  • Important: No credit if your code does not compile with the given Stack.h header file.
  • Important: You are not allowed to add any additional functions to your Queue class. Your Queue must contain only the functions from Lab 3 and these functions should not be altered from Lab 3. No credit if you add any additional functions to your Queue class or alter (other than correcting mistakes) the functionality of your Queue functions from Lab 3.
  • No credit if your code does not compile.