stack.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <iostream>
  2. #define SIZE 10
  3. using namespace std;
  4. struct stack {
  5. int count;
  6. int data[SIZE];
  7. };
  8. inline void stack_init(struct stack & the_stack) {
  9. the_stack.count = 0;
  10. }
  11. inline void stack_dropOut(struct stack & the_stack) {
  12. for (int c = 0; c < SIZE; ++c)
  13. the_stack.data[c] = the_stack.data[c + 1];
  14. --the_stack.count;
  15. }
  16. inline bool stack_isEmpty(struct stack & the_stack) {
  17. return (the_stack.count == 0);
  18. }
  19. inline void stack_push(struct stack & the_stack, const int item) {
  20. if (the_stack.count == SIZE)
  21. stack_dropOut(the_stack);
  22. the_stack.data[the_stack.count] = item;
  23. ++the_stack.count;
  24. }
  25. inline int stack_pop(struct stack & the_stack) {
  26. if (stack_isEmpty(the_stack)) {
  27. cout << "Error: Empty Collection Exception" << endl;
  28. return(-1);
  29. }
  30. --the_stack.count;
  31. return (the_stack.data[the_stack.count]);
  32. }
  33. inline int stack_peek(struct stack & the_stack) {
  34. return (the_stack.data[the_stack.count]);
  35. }
  36. inline bool stack_size(struct stack & the_stack) {
  37. return (the_stack.count);
  38. }
  39. inline void stack_toString(struct stack & the_stack) {
  40. for (int c = 0; c < the_stack.count; ++c)
  41. cout << "Item " << c + 1 << ": " << the_stack.data[c] << endl;
  42. }