chefbc
02-28-04, 08:16 PM
For some reason this doesn't work any suggestions
// stackADT.cc
#include <iostream>
#include "stackADT.h"
using namespace std;
// ======================
// StackADT
// ======================
// constructor
template <typename T> StackADT<T> :: StackADT()
{
stack = new Node<T>(); // header node
stackSize = 0;
}
// ======================
// StackADT
// ======================
// destructor
template <typename T> StackADT<T> :: ~StackADT()
{
makeEmpty();
delete stack;
}
// ======================
// top
// ======================
// returns the data item on the top of the stack
template <typename T> T StackADT<T> :: top()
{
Node<T> * temp;
T data;
if(stack != NULL)
{
temp = stack->next;
data = temp->data;
}
return data;
}
// ======================
// pop
// ======================
// pops or deletes items from the top of the stack
template <typename T> void StackADT<T> :: pop()
{
Node<T> * temp;
if(stack != NULL)
{
temp = stack->next;
delete stack;
stack = temp;
}
}
// ======================
// push
// ======================
// pushes or puts item onto the top of stack
template <typename T> void StackADT<T> :: push(T val)
{
Node<T> * new_item = new Node<T>(val);
Node<T> * temp;
if (!new_item) fatalError("StackADT :: push : new Failed");
if (stack->next == NULL) // if stack is empty
stack = new_item;
else{ // items already in stack
temp = stack;
stack = new_item;
new_item->next = temp;
}
}
// ======================
// size
// ======================
// returns the number of items currently in the stack
template <typename T> int StackADT<T> :: size()
{
return stackSize;
}
// ======================
// makeEmpty
// ======================
// return the data nodes of a list to the heap,
// set the next field in the header node to NULL
template <typename T> void StackADT<T> :: makeEmpty()
{
Node<T> * tmp;
Node<T> * ptr = stack->next;
while ( ptr != NULL ) {
tmp = ptr->next;
delete ptr;
ptr = tmp;
}
stackSize = 0;
}
// ======================
// empty
// ======================
// returns true if stack is empty, otherwise false
template <typename T> bool StackADT<T> :: empty()
{
if(stackSize == 0)
return true;
else
return false;
}
// ======================
// isFull
// ======================
// return true if the stack is full, otherwise false
template <typename T> bool StackADT<T> :: isFull()
{
Node<T> * tmp = new Node<T>();
if ( tmp == NULL ) return true;
delete tmp;
return false;
}
// ======================
// fatalError
// ======================
// print error message and then halt execution
template <typename T> void StackADT<T> :: fatalError( string msg )
{
cerr << "FATAL ERROR : " << msg << endl;
exit(1);
}
// stackADT.cc
#include <iostream>
#include "stackADT.h"
using namespace std;
// ======================
// StackADT
// ======================
// constructor
template <typename T> StackADT<T> :: StackADT()
{
stack = new Node<T>(); // header node
stackSize = 0;
}
// ======================
// StackADT
// ======================
// destructor
template <typename T> StackADT<T> :: ~StackADT()
{
makeEmpty();
delete stack;
}
// ======================
// top
// ======================
// returns the data item on the top of the stack
template <typename T> T StackADT<T> :: top()
{
Node<T> * temp;
T data;
if(stack != NULL)
{
temp = stack->next;
data = temp->data;
}
return data;
}
// ======================
// pop
// ======================
// pops or deletes items from the top of the stack
template <typename T> void StackADT<T> :: pop()
{
Node<T> * temp;
if(stack != NULL)
{
temp = stack->next;
delete stack;
stack = temp;
}
}
// ======================
// push
// ======================
// pushes or puts item onto the top of stack
template <typename T> void StackADT<T> :: push(T val)
{
Node<T> * new_item = new Node<T>(val);
Node<T> * temp;
if (!new_item) fatalError("StackADT :: push : new Failed");
if (stack->next == NULL) // if stack is empty
stack = new_item;
else{ // items already in stack
temp = stack;
stack = new_item;
new_item->next = temp;
}
}
// ======================
// size
// ======================
// returns the number of items currently in the stack
template <typename T> int StackADT<T> :: size()
{
return stackSize;
}
// ======================
// makeEmpty
// ======================
// return the data nodes of a list to the heap,
// set the next field in the header node to NULL
template <typename T> void StackADT<T> :: makeEmpty()
{
Node<T> * tmp;
Node<T> * ptr = stack->next;
while ( ptr != NULL ) {
tmp = ptr->next;
delete ptr;
ptr = tmp;
}
stackSize = 0;
}
// ======================
// empty
// ======================
// returns true if stack is empty, otherwise false
template <typename T> bool StackADT<T> :: empty()
{
if(stackSize == 0)
return true;
else
return false;
}
// ======================
// isFull
// ======================
// return true if the stack is full, otherwise false
template <typename T> bool StackADT<T> :: isFull()
{
Node<T> * tmp = new Node<T>();
if ( tmp == NULL ) return true;
delete tmp;
return false;
}
// ======================
// fatalError
// ======================
// print error message and then halt execution
template <typename T> void StackADT<T> :: fatalError( string msg )
{
cerr << "FATAL ERROR : " << msg << endl;
exit(1);
}