Guest User

Untitled

a guest
Sep 11th, 2011
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.66 KB | None | 0 0
  1. //Files originally from the book files, I have modified it for the assignment
  2. //Header file: myStack.h
  3.  
  4. #ifndef H_StackType
  5. #define H_StackType
  6.  
  7. #include <iostream>
  8. #include <cassert>
  9.  
  10. #include "stackADT.h"
  11.  
  12. using namespace std;
  13.  
  14. template <class Type>
  15. class stackType: public stackADT<Type>
  16. {
  17. public:
  18.     const stackType<Type>& operator=(const stackType<Type>&);
  19.       //Overload the assignment operator.
  20.  
  21. //start of my code *************************************
  22.  
  23.     bool operator==(stackType<Type>&);
  24.  
  25.  
  26. //end of my code ***************************************
  27.  
  28.     void initializeStack();
  29.       //Function to initialize the stack to an empty state.
  30.       //Postcondition: stackTop = 0
  31.  
  32.     bool isEmptyStack() const;
  33.       //Function to determine whether the stack is empty.
  34.       //Postcondition: Returns true if the stack is empty,
  35.       //               otherwise returns false.
  36.  
  37.     bool isFullStack() const;
  38.       //Function to determine whether the stack is full.
  39.       //Postcondition: Returns true if the stack is full,
  40.       //               otherwise returns false.
  41.  
  42.     void push(const Type& newItem);
  43.       //Function to add newItem to the stack.
  44.       //Precondition: The stack exists and is not full.
  45.       //Postcondition: The stack is changed and newItem
  46.       //               is added to the top of the stack.
  47.  
  48.     Type top() const;
  49.       //Function to return the top element of the stack.
  50.       //Precondition: The stack exists and is not empty.
  51.       //Postcondition: If the stack is empty, the program
  52.       //               terminates; otherwise, the top element
  53.       //               of the stack is returned.
  54.  
  55.     void pop();
  56.       //Function to remove the top element of the stack.
  57.       //Precondition: The stack exists and is not empty.
  58.       //Postcondition: The stack is changed and the top
  59.       //               element is removed from the stack.
  60.  
  61.     stackType(int stackSize = 100);
  62.       //constructor
  63.       //Create an array of the size stackSize to hold
  64.       //the stack elements. The default stack size is 100.
  65.       //Postcondition: The variable list contains the base
  66.       //               address of the array, stackTop = 0, and  
  67.       //               maxStackSize = stackSize.
  68.  
  69.     stackType(const stackType<Type>& otherStack);
  70.       //copy constructor
  71.  
  72.     ~stackType();
  73.       //destructor
  74.       //Remove all the elements from the stack.
  75.       //Postcondition: The array (list) holding the stack
  76.       //               elements is deleted.
  77.  
  78. private:
  79.     int maxStackSize; //variable to store the maximum stack size
  80.     int stackTop;     //variable to point to the top of the stack
  81.     Type *list;       //pointer to the array that holds the
  82.                       //stack elements
  83.  
  84.     void copyStack(const stackType<Type>& otherStack);
  85.       //Function to make a copy of otherStack.
  86.       //Postcondition: A copy of otherStack is created and
  87.       //               assigned to this stack.
  88. };
  89.  
  90.  
  91. template <class Type>
  92. void stackType<Type>::initializeStack()
  93. {
  94.     stackTop = 0;
  95. }//end initializeStack
  96.  
  97. template <class Type>
  98. bool stackType<Type>::isEmptyStack() const
  99. {
  100.     return(stackTop == 0);
  101. }//end isEmptyStack
  102.  
  103. template <class Type>
  104. bool stackType<Type>::isFullStack() const
  105. {
  106.     return(stackTop == maxStackSize);
  107. } //end isFullStack
  108.  
  109. template <class Type>
  110. void stackType<Type>::push(const Type& newItem)
  111. {
  112.     if (!isFullStack())
  113.     {
  114.         list[stackTop] = newItem;   //add newItem to the
  115.                                     //top of the stack
  116.         stackTop++; //increment stackTop
  117.     }
  118.     else
  119.         cout << "Cannot add to a full stack." << endl;
  120. }//end push
  121.  
  122. template <class Type>
  123. Type stackType<Type>::top() const
  124. {
  125.     assert(stackTop != 0);          //if stack is empty,
  126.                                     //terminate the program
  127.     return list[stackTop - 1];      //return the element of the
  128.                                     //stack indicated by
  129.                                     //stackTop - 1
  130. }//end top
  131.  
  132. template <class Type>
  133. void stackType<Type>::pop()
  134. {
  135.     if (!isEmptyStack())
  136.         stackTop--;                 //decrement stackTop
  137.     else
  138.         cout << "Cannot remove from an empty stack." << endl;
  139. }//end pop
  140.  
  141. template <class Type>
  142. stackType<Type>::stackType(int stackSize)
  143. {
  144.     if (stackSize <= 0)
  145.     {
  146.         cout << "Size of the array to hold the stack must "
  147.              << "be positive." << endl;
  148.         cout << "Creating an array of size 100." << endl;
  149.  
  150.         maxStackSize = 100;
  151.     }
  152.     else
  153.         maxStackSize = stackSize;   //set the stack size to
  154.                                     //the value specified by
  155.                                     //the parameter stackSize
  156.  
  157.     stackTop = 0;                   //set stackTop to 0
  158.     list = new Type[maxStackSize];  //create the array to
  159.                                     //hold the stack elements
  160. }//end constructor
  161.  
  162. template <class Type>
  163. stackType<Type>::~stackType() //destructor
  164. {
  165.     delete [] list; //deallocate the memory occupied
  166.                     //by the array
  167. }//end destructor
  168.  
  169. template <class Type>
  170. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  171. {
  172.     delete [] list;                
  173.     maxStackSize = otherStack.maxStackSize;        
  174.     stackTop = otherStack.stackTop;            
  175.      
  176.     list = new Type[maxStackSize];                     
  177.  
  178.         //copy otherStack into this stack
  179.     for (int j = 0; j < stackTop; j++)  
  180.         list[j] = otherStack.list[j];
  181. } //end copyStack
  182.  
  183.  
  184. template <class Type>
  185. stackType<Type>::stackType(const stackType<Type>& otherStack)
  186. {
  187.     list = NULL;
  188.  
  189.     copyStack(otherStack);
  190. }//end copy constructor
  191.  
  192. template <class Type>
  193. const stackType<Type>& stackType<Type>::operator=
  194.                     (const stackType<Type>& otherStack)
  195. {
  196.     if (this != &otherStack) //avoid self-copy
  197.         copyStack(otherStack);
  198.  
  199.     return *this;
  200. } //end operator=        
  201.  
  202. //start of my code *************************************
  203.  
  204. template <class Type>
  205. bool stackType<Type>::operator==
  206.                     (stackType<Type>& stacker)
  207. {
  208.     stackType<Type> stackA, stackB;
  209.     bool result = false;
  210.     stackA.copyStack(this);
  211.     stackB.copyStack(stacker);
  212.    
  213.     while(!stackA.isEmptyStack() && !stackB.isEmptyStack())
  214.     {
  215.         if(stackA.top() == stackB.top())
  216.         {
  217.             stackA.pop();
  218.             stackB.pop();
  219.             if(stackA.isEmptyStack() && stackB.isEmptyStack())
  220.             {result = true;}
  221.         }
  222.         else
  223.         {result = false;}
  224.        
  225.     }
  226.  
  227.     return result;
  228.    
  229. }
  230.  
  231.  
  232. //end of my code ***************************************
  233.  
  234.  
  235. #endif
Advertisement
Add Comment
Please, Sign In to add comment