Posted on February 5, 2010 by kturley Definition file, stack.h #ifndef STACK_H #define STACK_H #include <string> using namespace std; typedef string ItemType; // Change the datatype of the stack here //------------------------------------------------------------------------- // Class: Stack // Class implements a doubly linked list of string items. //------------------------------------------------------------------------- class Stack { private: class Node; typedef Node* NodePtr; //------------------------------------------------------------------------- // Class: Node // A doubly linked list node. //------------------------------------------------------------------------- class Node { public: ItemType item; NodePtr previous; NodePtr next; Node(const ItemType& Item, const NodePtr Previous, const NodePtr Next) { item = Item; previous = Previous; next = Next; } }; NodePtr head; // Deletes all the nodes on the list. Head is set to NULL. void DeleteNodes(); // Deep copies each node in the parameter linked list. void CopyNodes(NodePtr CopyHead); // Returns a node populated with supplied parameters NodePtr NewNode(const ItemType& Item, const NodePtr Previous, const NodePtr Next); public: // Sets the head to NULL. Stack() : head(NULL) {} // Makes a deep copy of the parameter stack. Stack(const Stack& CopyStack); // Destroys the entire list. ~Stack(); // Returns true if list is empty, false otherwise. bool IsEmpty() const { return (head == NULL); } // Returns the item at the top of the stack. ItemType TopItem() const { return (head->item); } // Inserts a new node containing Value at the front of the list. void Push(const ItemType& Value); // Removes the first item from the list. void Pop(); // Makes deep copy of the rhs stack. Stack operator =(const Stack& rhs); }; #endif Implementation file, stack.cpp #include "stack.h" #include <stdexcept> #include <iostream> using namespace std; // Deletes all the nodes on the list. Head is set to NULL. void Stack::DeleteNodes() { NodePtr pDel = head; while (head != NULL) { head = head->next; pDel->previous = pDel->next = NULL; delete pDel; pDel = head; } } // Deep copies each node in the parameter linked list. void Stack::CopyNodes(NodePtr CopyHead) { NodePtr pTail; head = pTail = NewNode(CopyHead->item, NULL, NULL); CopyHead = CopyHead->next; while(CopyHead != NULL) { pTail = pTail->next = NewNode(CopyHead->item, pTail, NULL); CopyHead = CopyHead->next; } pTail = NULL; } // Returns a node populated with supplied parameters Stack::NodePtr Stack::NewNode(const ItemType& Item, const NodePtr Previous, const NodePtr Next) { NodePtr pNew; try { pNew = new Node(Item, Previous, Next); if (pNew == NULL) throw bad_alloc("Unable to allocate memory."); } catch (bad_alloc e) { cerr < < e.what() << endl; } return (pNew); } // Makes a deep copy of the parameter stack. Stack::Stack(const Stack& CopyStack) { CopyNodes(CopyStack.head); } // Destroys the entire list. Stack::~Stack() { DeleteNodes(); } // Inserts a new node containing Value at the front of the list. void Stack::Push(const ItemType& Value) { if (head == NULL) { head = NewNode(Value, NULL, NULL); } else { head = head->previous = NewNode(Value, NULL, head); } } // Removes the first list from the list. void Stack::Pop() { NodePtr pDel = head; head = head->next; pDel->previous = pDel->next = NULL; delete pDel; pDel = NULL; } // Makes deep copy of the rhs stack. Stack Stack::operator =(const Stack& rhs) { if (this != &rhs) { DeleteNodes(); CopyNodes(rhs.head); } return (*this); } Test driver //----------------------------------------------------------------------- // Name: Kyle Turley // // Date: Feb 5 2010 // // Description: A doubly linked stack class driver //----------------------------------------------------------------------- #include "stack.h" #include <iostream> using namespace std; void main() { Stack EmptyStack; cout < < (EmptyStack.IsEmpty()? "Stack is empty." : "Stack is not empty.") << endl; Stack stack1; stack1.Push("Hello"); stack1.Push("There"); cout << stack1.TopItem() << endl << endl; Stack stack2(stack1); stack2.Pop(); cout << "Stack1 top item: " << stack1.TopItem() << endl << "Stack2 top item: " << stack2.TopItem() << endl << endl; Stack stack3; stack3 = stack1; stack3.Push("NoWay"); cout << "Stack1 top item: " << stack1.TopItem() << endl << "Stack3 top item: " << stack3.TopItem() << endl << endl; }