// File: intstack.C // // Implementation of class IntStack which was derived from GenStack #include #include #include #include "intstack.h" // "Local" functions // static void CrashOnNULL(void *ptr, char *mesg) ; static void CrashOnNULL(void *ptr, char *mesg) { if (ptr == NULL) { cerr << "CrashOnNULL: " << mesg << endl ; exit(1) ; } } // Default constructor // IntStack::IntStack() : GenStack(cmp, prt) // member initializer { #ifndef NDEBUG cerr << "IntStack constructor, this = " << this << endl ; #endif // Do nothing } // Destructor // IntStack::~IntStack() { #ifndef NDEBUG cerr << "IntStack destructor, this = " << this << endl ; #endif // Do nothing, GenStack destructor automatically called } // Add an int to the top of stack // void IntStack::push(int n) { int *ptr = new int(n) ; CrashOnNULL(ptr, "Out of memory in IntStack::push()") ; GenStack::push(ptr) ; } // Remove and return int from top of stack // int IntStack::pop() { int result, *ptr ; ptr = (int *) GenStack::pop() ; result = *ptr ; delete ptr ; return result ; } // Return (don't remove) int from top of stack // int IntStack::top() { int *ptr ; ptr = (int *) GenStack::top() ; return *ptr ; } // Compare two integers // int IntStack::cmp(void *vptr1, void *vptr2) { int *ptr1, *ptr2 ; ptr1 = (int *) vptr1 ; ptr2 = (int *) vptr2 ; if (ptr1 == NULL && ptr2 == NULL) return 0 ; if (ptr1 == NULL) return -1 ; if (ptr2 == NULL) return 1 ; if (*ptr1 < *ptr2) return -1 ; if (*ptr1 == *ptr2) return 0 ; return 1 ; } // print out an integer // void IntStack::prt(void *vptr) { int *ptr ; if (vptr == NULL) return ; ptr = (int *) vptr ; cout << *ptr << " " ; }