//template for Stack abstract data type //Updated 19 May 2012 //Found a bug? Awesome! Let us know via: http://generaldevelopment.net/contact /*============================================================================== Copyright (c) 2011 General Development Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ==============================================================================*/ #pragma once #include #include template class Stack { private: template class StackNode { public: U element; StackNode* next; //next node down. NULL if we're on the bottom. }; StackNode* top; void copy(Stack& src) { //push another complete stack onto this one. If *this is empty initially, it will become a copy of src. StackNode* srcNode; StackNode* newTop; StackNode* newNode; //Step 1: duplicate the linked list. if(!src.top) return; //nothing to be done. newNode = new StackNode; newTop = newNode; srcNode = src.top; while(true) { //loop condition is a non-null source node, but check it half way through to save code newNode->element = srcNode->element; srcNode = srcNode->next; if(!srcNode) break; //check loop condition here newNode->next = new StackNode; newNode = newNode->next; } //Step 2: append the current list's top to the end of the new list newNode->next = top; //Step 3: place new stack on existing stack top = newTop; } public: class StackUnderflow : public std::exception { public: virtual const char* what() const throw() { return "Stack underflow in Stack ADT"; } }; Stack() { top = 0; } Stack(Stack& src) : Stack() { //copy constructor copy(src); } Stack& operator=(Stack& src) { clear(); copy(src); return *this; //by C++ convention. } ~Stack() { clear(); } void clear() { //clear this stack object while(!isEmpty()) pop(); } void push(T a) { StackNode* n = new StackNode; n->next = top; n->element = a; top = n; } T pop() { if(!top) throw StackUnderflow(); T ret = top->element; StackNode* n = top; top = n->next; delete n; return ret; } T peek() { if(!top) throw StackUnderflow(); return top->element; } bool isEmpty() { return !top; } bool isPresent(T a) { //Provides a search function for the stack. //returns true iff a is present in the stack at all, top or otherwise StackNode* n = top; while(n) { if(n->element == a) return true; n = n->next; } return false; } };