#include #include #include "stack.h" using namespace std; /* Default constructor*/ template stack::stack() { stackTop = NULL; size = 0; } /* Destructor */ template stack::~stack() { } /* Display for testing */ template void stack::display() { node *current = stackTop; cout << "Top --> "; while(current != NULL) { cout<data<< " "; current = current -> next; } cout < bool stack::isEmptyStack() { return (stackTop == NULL); } /* Clear stack */ template void stack::clear() { stackTop = NULL; } /* Add new item to the stack */ template void stack::push(Type item) { node *newNode; newNode = new node; newNode->data = item; newNode->next = stackTop; stackTop = newNode; size++; } /* Return the top element of the stack */ template Type stack::top() { assert(stackTop != NULL); return stackTop->data; } /* Remove the top element of the stack */ template void stack::pop() { node *temp; if(!isEmptyStack()) { temp = stackTop; stackTop = stackTop->next; delete temp; size--; } else { cout << "Stack is empty !" << endl; } } /* Operator "=" */ template stack stack::operator=(stack & otherStack) { node *newNode, *current, *last; if (stackTop != NULL) /* If stack is no empty, make it empty */ stackTop = NULL; if (otherStack.stackTop == NULL) stackTop = NULL; else { current = otherStack.stackTop; stackTop = new node; stackTop->data = current->data; stackTop->next = NULL; last = stackTop; current = current ->next; /* Copy the remaining stack */ while (current != NULL) { newNode = new node; newNode->data = current->data; newNode->next = NULL; last->next = newNode; last = newNode; current = current->next; } } size = otherStack.size; return *this; }