This is part of an assignment that I am doing. I have to create a Queue (FIFO) using templates. I have been able to create that and it works. There is one more part to this same assignment where I am unable to figure out how to proceed. Here is what I need to do -
Add a second template parameter that specifies the data structure to be used in the template Stack class. The implementation should be container based (use any one from STL).
Here is an extract from the first part of code (creating Queue with template so it can be used with char, int, float):
class Stack
{
public:
Stack(int=5); // Constructor
~Stack(); // Destructor
void push(const T); // add a character to the back of the queue
bool empty() const; // test if the queue is empty
bool full() const; // test if the queue is full
T pop(); // remove a character from the front of the queue
T front() const; // check, but do not remove, the character at the front of the queue
int size() const; // returns the number of elements in the queue
void shrink(); // resize the queue to be as small as possible
void expand(); // Expand the queue to accomodate more characters
int capacity(); // returns the number of elements allocated
void resize(int); // resize the queue by adding some elements;
private:
T *_data;
int _capacity;
int _front; // = 0;
int _back; // = 0;
int _size; // = 0;
static const int _EMPTY = -1;
static const int _DEFAULT = 5;
};
template <class T>
Stack<T>::Stack(int capacity)
{
if (capacity < 0)
{
_capacity = _DEFAULT;
}
else
{
_capacity = capacity; // Total size of the array
_data = new T[_capacity]; // Allocate memory for the array
_front = 0;
_back = 0;
_size = 0;
}
}
template <class T>
Stack<T>::~Stack()
{
delete [] _data;
}
template <class T>
bool Stack<T>::empty() const
{
return _size == 0;
}
template <class T>
bool Stack<T>::full() const
{
return _size == _capacity;
}
template <class T>
void Stack<T>::push(const T c)
{
if (_size==_capacity) expand();
_data[_back] = c;
if (_back++==_capacity-1) _back=0;
_size++;
}
template <class T>
T Stack<T>::pop()
{
if (_size==0) return _EMPTY;
T c = _data[_front];
if (_front++==_capacity-1) _front=0;
_size--;
return c;
}
How should I proceed converting the same stuff above to use another template parameter that specifies the data structure to be used in the template Stack class? How will the function definitions change?
Thanks
k