Implement Stack in C Array
Check out all articles in Technical Article Structure on Medium
Stack is a linear data structure with an order of FILO, which means First In Last Out.
It is common to see stack in real world, such as plates in a restaurant. The plate at the bottom is the first to put in, but will be the last one to be used. Stack is also present at the bottle that you store your coffee beans, or even your Pringles.
In programming world, stack is used when the processing order is not important, or when the purpose of that program is similar to checking syntax correctness. Picture below is from Geeks for Geeks. I find it very useful to understand the logic of stack.
To implement stack in programming language, we often need to code 6 functions.
- createStack — this function will be used to allocate memory and initialize stack, where stack is a custom class
- isEmpty — this function returns TRUE if the stack is empty
- isFull — this function returns TRUE if the stack is full
- Push — to add a new item to the top of the stack
- Pop — to retrieve an item from the top, and remove it from the stack
- Top — to see the item on the top of the stack