Advertisement
Cinestra

Trying to figure out implementing Stacks with Coord myself

Mar 20th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Coord.h"
  4.  
  5. const int SIZE = 100;
  6.  
  7. class Stack
  8. {
  9. public:
  10. Stack()
  11. {
  12. // The first item should go in the 0th slot of the array
  13. m_top = 0;
  14. }
  15.  
  16. void push(int r, int c)
  17. {
  18. if (m_top >= SIZE)
  19. // If the top item is at the size limit or somehow above, exit
  20. exit(-1);
  21.  
  22. // Set the top item equal to i and then increment the top to point at the next available space
  23. m_stack[m_top++] = new Coord(r, c);
  24. }
  25.  
  26. Coord* pop()
  27. {
  28. if (m_top == 0)
  29. exit(-1);
  30.  
  31. // The top actually refers to the next available open space
  32. // So we will first decrement to the most recent spot that has a value
  33. // Return the value
  34. // Then when we try to put something on the stack again, we will overwrite this data point
  35. // The data point isn't actually goint to be erased here, but we will make it easy to overwrite over it
  36. return m_stack[--m_top];
  37. }
  38.  
  39. bool is_empty(void)
  40. {
  41. return (m_top == 0);
  42. }
  43.  
  44. Coord* peek_top()
  45. {
  46. if (m_top == 0)
  47. exit(-1);
  48.  
  49. return m_stack[m_top - 1];
  50. }
  51.  
  52. private:
  53. Coord* m_stack[SIZE];
  54. int m_top;
  55. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement