Advertisement
darkhelmet125

StackADT header

Oct 18th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. /*Matt Short
  2. Assignment 4
  3. Purpose: create a template stack ADT class
  4. CPSC 131*/
  5. //SPECIFICATION FILE
  6. template<class dataType>
  7. class stackADT
  8. {
  9. public:
  10.     stackADT(); //constructor
  11.     bool isEmpty() const;
  12.     /*Purpose: Determines wheather the stack is empty.
  13.     Precondition: Stack has been initialized.
  14.     Postcondition: Function value = (stack is empty).*/
  15.     bool isFull() const;
  16.     /*Purpose: Determines wheather the stack is full.
  17.     Precondition: Stack has been initialized.
  18.     Postcondition: Function calue = (stack is full).*/
  19.     void Push(dataType newItem);
  20.     /*Purpose: Adds newItem to the top of the stack
  21.     Precondition: Stack has been initialized
  22.     Postcondition: If (stack is full), exception FullStack is thrown, else newItem is at the top of the stack.*/
  23.     void Pop();
  24.     /*Purpose: Removes top item from the stack.
  25.     Precondition: Stack has been initialized.
  26.     Postcondition: If (stack is full), exception FullStack is thrown, else newItem is at the top of the stack.*/
  27.     dataType Top() const;
  28.     /*Purpose: Returns a copy of the top item on the stack.
  29.     Precondition: Stack has been initialized.
  30.     Postconditions: If (stack is empty), exception EmptyStack is thrown, else a copy of the top element is returned.*/
  31.     ~stackADT(); //deconstructor
  32. private:
  33.     int *link;
  34.     dataType info;
  35.     int top;
  36.     dataType *items;
  37. };
  38.  
  39. //IMPLEMENTATION FILE
  40. #include<iostream>
  41. using namespace std;
  42. template<class dataType>
  43. stackADT<dataType>::stackADT()
  44. {
  45.     max=200;
  46.     items=new dataType[max];
  47.     top=-1;
  48. }
  49. template<class dataType>
  50. bool stackADT<dataType>::isEmpty() const
  51. {
  52.     if(top==-1)
  53.         return true;
  54.     else
  55.         return false;
  56. }
  57. template<class dataType>
  58. bool stackADT<dataType>::isFull() const
  59. {
  60.     if(top==max-1)
  61.         return true;
  62.     else
  63.         return false;
  64. }
  65. template<class dataType>
  66. void stackADT<dataType>::Push(dataType newItem)
  67. {
  68.     if(isFull())
  69.     {
  70.         cout<<"Stack is full."<<endl;
  71.         return;
  72.     }
  73.     else
  74.     {
  75.         top++;
  76.         items[top]=newItem;
  77.     }
  78. }
  79. template<class dataType>
  80. void stackADT<dataType>::Pop()
  81. {
  82.     if(isEmpty())
  83.     {
  84.         cout<<"Stack is Empty."<<endl;
  85.         return;
  86.     }
  87.     else
  88.     {
  89.         temp = new
  90.         temp->link=top->link;
  91.         top->link=top->link;
  92.         delete temp;
  93.         top--;
  94.     }
  95. }
  96. template<class dataType>
  97. dataType stackADT<dataType>::Top() const
  98. {
  99.     if(!isEmpty())
  100.         return top->info;
  101. }
  102. template<class dataType>
  103. stackADT<dataType>::~stackADT()
  104. {
  105.     while(!isEmpty)
  106.     {
  107.         Pop();
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement