/****************************************************************************/ /* Sample C++ "dynamic stack" tester demonstrating the usage of DStack. */ /* The DStack struct is declared and functions are prototyped in dstack.h.*/ /* The C++ code for the functions is in dstack.cc. */ /* */ /* Compile with: */ /* g++ -o tdstack tdstack.cc dstack.cc */ /* or: */ /* CC -o tdstack tdstack.cc dstack.cc */ /* */ /* Usage: */ /* tdstack */ /* */ /****************************************************************************/ #include #include "dstack.h" int main() { DStack S; init(S,4); // Here's a function for printing a DStack: cout << "\n" << S << "\n"; push(S, 2.31); push(S, 1.19); push(S, 6.78); push(S, 0.54); cout << S << "\n"; if (!full(S)) push(S, 6.7); // this should do nothing, as // stack is already full. cout << S << "\n"; cout << "Popped value is: " << pop(S) << "\n"; cout << S; push(S, pop(S) + pop(S)); cout << "\nReplace top two items with their sum: \n"; cout << S << "\n"; pop(S); pop(S); cout << S << "\n"; if (!empty(S)) pop(S); // this should also do nothing, // as stack is already empty. if (num_items(S) != 0) { cout << "Error: Stack is corrupt!\n"; } // Here's an example of freeing the storage from a DStack: remove(S); return 0; }