A stack is a data structure that follows the Last In First Out (LIFO) principle, meaning the last element added to the stack is the first one to be removed. It can be visualized as a stack of plates where you can only add or remove the top plate.
4
1
24
8
9
Here, only the last element added can be removed, which is the value 4.
We want to add the value 33 to the stack
4
1
24
8
9
We simply push the new value onto the top of the stack.
33
4
1
24
8
9
We want to remove (and get) the last value of the stack
4
1
24
8
9
We simply pop the last value from the stack, 4, and return it.
1
24
8
9