1 /* Roy Keene 2 CS 2314 3 Section 04 4 Lab 03 5 03 Sept 02 6 stack.cpp 7 */ 8 9 #include <iostream> 10 #include <string> 11 12 using namespace std; 13 #include "stack.h" 14 15 Stack::Stack(void) { 16 #ifdef _STACK_GROWABLE 17 StackData=new int[_STACK_STACKSIZE]; 18 StackSize=_STACK_STACKSIZE; 19 #endif 20 StackLocation=-1; 21 return; 22 } 23 24 Stack::~Stack(void) { 25 #ifdef _STACK_GROWABLE 26 delete StackData; 27 #endif 28 return; 29 } 30 31 void Stack::Push(int data) { 32 #ifdef _STACK_GROWABLE 33 int *tmpstack; 34 int i; 35 36 if ((StackLocation+1)>((signed int) StackSize)) { 37 StackSize*=2; 38 /* There seems to be a bug here when StackSize==5 */ 39 tmpstack=new int[StackSize]; 40 memcpy(tmpstack, StackData, (StackLocation+1)*sizeof(int)); 41 delete StackData; 42 StackData=tmpstack; 43 } 44 #endif 45 if (Full()) return; 46 StackData[++StackLocation]=data; 47 } 48 49 int Stack::Pop(void) { 50 if (Empty()) return(-1); 51 return(StackData[StackLocation--]); 52 } 53 54 int Stack::Top(void) { 55 if (Empty()) return(-1); 56 return(StackData[StackLocation]); 57 } 58 59 bool Stack::Empty(void) { 60 return(StackLocation==-1); 61 } 62 63 bool Stack::Full(void) { 64 #ifdef _STACK_GROWABLE 65 return(false); 66 #else 67 return(StackLocation==_STACK_STACKSIZE); 68 #endif 69 } |