Question: Implement classes Stack and Queue that provide the stack and queue functionalities needed by the test code Stack_test.cpp and Queue_test.cpp respectivelly. Stack_test.cpp is given below.

Implement classes Stack and Queue that provide the stack and queue functionalities needed by the test code Stack_test.cpp and Queue_test.cpp respectivelly. Stack_test.cpp is given below. You are to give your own Queue_test.cpp. When you do this, you should not use STL Stack or Queue class.

First get the simpler test program test.cpp given below working, then get Stack_test.cpp working.

Remember when you need to compile a program that needs class String from lab 3, you must include String.h and compile as follows:

$ g++ Stack_test.cpp ../lab3/String.o 
#ifndef STACK_H #define STACK_H // Stack.h -- a stack implemented as an adapter (of vector or list or ...) #include "../lab5/List.h" using namespace std; //Use the following line for STL containers. //template  > class Container = list> template  class Container = List> class Stack { public: //We don't need a constructor or destructor because the Container has/should have one //Stack(): container() { } //~Stack() { ~container(); } bool empty() const; unsigned int size() const; void push(const T & x); void pop(); T & top(); private: Container container; }; #endif 
// Stack_test.cpp #include  #include  #include "Stack.h" #include "../lab3/String.h" #include "../lab4/Vector.h" #include "../lab5/List.h" using namespace std; int main() { Stack s1; assert(s1.size() == 0); assert(s1.empty()); s1.push(16); assert(s1.size() == 1); assert(s1.top() == 16); s1.pop(); assert(s1.size() == 0); s1.push(11); assert(s1.size() == 1); assert(s1.top() == 11); s1.push(22); assert(s1.size() == 2); assert(s1.top() == 22); s1.push(33); assert(s1.size() == 3); assert(s1.top() == 33); s1.pop(); assert(s1.size() == 2); assert(s1.top() == 22); Stack s2; s2.push("abc"); s2.push("de"); s2.pop(); assert(s2.top() == "abc"); cout << "SUCCESS "; } 
// test.cpp - a simple test program for Stack.h #include  #include "../lab4/Vector.h" #include "Stack.h" using namespace std; main() { Stack s; // uses List as the default container s.push(5); s.push(6); cout << s.top() << endl; Stack v; // uses Vector as the container v.push(1.5); v.push(2.3); v.pop(); cout << v.top() << endl; } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!