CSCI 330 Lab: Stack and Queue

Complete the implementation of the mystack template class as defined below so that it runs correctly with the test code test_my_stack.cpp.

Create a my_queue template class (in my_queue.h) and test code for it (in test_my_queue.cpp). Implement functionality similar to my_stack. (In stacks, we use the name top for the function that allows us to access an element in the container; in queues, we use the name front for the function that provides this.)

Use mylist<T> for the default underlying container for my_queue. This means you will need to implement the push_back and pop_front functions of the my_list data structure (unless you already implemented them as part of the previous lab).

Submit your test code for my_queue along with your implementations of my_stack and my_queue, but do not submit the test code provided below for my_stack.

#ifndef MY_STACK_H
#define MY_STACK_H

// my_stack.h  -- a stack implemented as an adapter of myvector

#include "my_vector.h"
using namespace std;

template <class T, class Container = my_vector<T> > 
class my_stack
{
public:
   typedef T value_type;
   my_stack();
   bool empty() const;
   unsigned int size() const;
   void push(const T & x); 
   void pop();
   T & top();

private:
   Container container;
};

#endif


// test_my_stack.cpp

#include <iostream>
#include <stack>
#include <vector>
#include <cassert>
#include "my_stack.h"
#include "my_vector.h"

using namespace std;

int main(char *args[])
{
   //#define STACK stack
   //#define VECTOR vector
   #define STACK my_stack
   #define VECTOR my_vector

   STACK<int> s1;

   STACK<int, VECTOR<int> > s2;
   assert(s2.size() == 0);
   assert(s2.empty());

   s2.push(16);
   assert(s2.size() == 1);
   assert(s2.top() == 16);

   s2.pop();
   assert(s2.size() == 0);

   s2.push(11);
   assert(s2.size() == 1);
   assert(s2.top() == 11);

   s2.push(22);
   assert(s2.size() == 2);
   assert(s2.top() == 22);

   s2.push(33);
   assert(s2.size() == 3);
   assert(s2.top() == 33);

   s2.pop();
   assert(s2.size() == 2);
   assert(s2.top() == 22);

   cout << "All tests passed." << endl;
   cin.get();
}