//Stack.cc ptr based stack implementation file with templates
#include <iostream.h>
#include <stddef.h>
#include <assert.h>
#include "Stack.h"
template <class T>
struct stacknode
{
T item;
stacknode<T>* next;
};
template <class T>
Stack<T>::Stack() : topptr(NULL){}
template <class T>
Stack<T>::Stack(const Stack<T>& s)
{
if (s.topptr == NULL)
topptr = NULL;
else
{
topptr=new stacknode<T>;
assert(topptr != NULL);
topptr->item = s.topptr->item;
stacknode<T>* newptr=topptr;
for (ptrtype origptr = s.topptr->next; origptr!=NULL;
origptr=origptr->next)
{
newptr->next=new stacknode<T>;
assert(newptr->next!=NULL);
newptr=newptr->next;
newptr->item=origptr->item;
}
newptr->next=NULL;
}
}
template <class T>
Stack<T>::~Stack()
{
bool success;
success=pop();
while(success)
success=pop();
assert(topptr == NULL);
}
template <class T>
bool Stack<T>::isempty() const
{
return bool(topptr == NULL);
}
template <class T>
bool Stack<T>::push(T newitem)
{
stacknode<T>* newptr=new stacknode<T>;
bool success=bool(newptr!=NULL);
if(success)
{
newptr->item=newitem;
newptr->next=topptr;
topptr=newptr;
}
return success;
}
template <class T>
bool Stack<T>::pop()
{
bool success=bool(!isempty());
if (success)
{
stacknode<T>* temp=topptr;
topptr=topptr->next;
temp->next=NULL;
delete temp;
}
return success;
}
template <class T>
bool Stack<T>::pop(T& stacktop)
{
bool success=bool(!isempty());
if (success)
{
stacktop=topptr->item;
stacknode<T>* temp=topptr;
topptr=topptr->next;
temp->next=NULL;
delete temp;
}
return success;
}
template <class T>
bool Stack<T>::gettop(T& stacktop)
{
bool success=bool(!isempty());
if(success)
stacktop=topptr->item;
return success;
}