/****************************************************************************/ /* Sample C++ "dynamic stack", as discussed in class. */ /* The DStack struct is declared and functions are prototyped in dstack.h.*/ /* Usage is demonstrated in tdstack.cc. */ /****************************************************************************/ #include #include "dstack.h" void init(DStack &S, int size) { S.bottom = new float[size]; /* the new way, new */ // S.bottom = (float *)malloc(size*sizeof(float));/* the old way, malloc()*/ S.top = S.bottom; S.size = size; } void remove(DStack &S) { delete [] S.bottom; /* the new way, delete */ // free((char *)S.bottom); /* the old way, free() */ } int num_items(const DStack &S) { return (S.top - S.bottom ); } void push(DStack &S, float val) { *S.top = val; S.top++; } float pop(DStack &S) { S.top--; return *S.top; } int full(const DStack &S) { return (num_items(S) >= S.size); } int empty(const DStack &S) { return (num_items(S) <= 0); } ostream& operator<<(ostream &os, const DStack &S) { /* the new way, a stream operator*/ os << "Stack currently holds " << num_items(S) << " items: " ; for (float *element=S.bottom; element