Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "Coord.h"
- const int SIZE = 100;
- class Stack
- {
- public:
- Stack()
- {
- // The first item should go in the 0th slot of the array
- m_top = 0;
- }
- void push(int r, int c)
- {
- if (m_top >= SIZE)
- // If the top item is at the size limit or somehow above, exit
- exit(-1);
- // Set the top item equal to i and then increment the top to point at the next available space
- m_stack[m_top++] = new Coord(r, c);
- }
- Coord* pop()
- {
- if (m_top == 0)
- exit(-1);
- // The top actually refers to the next available open space
- // So we will first decrement to the most recent spot that has a value
- // Return the value
- // Then when we try to put something on the stack again, we will overwrite this data point
- // The data point isn't actually goint to be erased here, but we will make it easy to overwrite over it
- return m_stack[--m_top];
- }
- bool is_empty(void)
- {
- return (m_top == 0);
- }
- Coord* peek_top()
- {
- if (m_top == 0)
- exit(-1);
- return m_stack[m_top - 1];
- }
- private:
- Coord* m_stack[SIZE];
- int m_top;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement