Originally published on CodingCush .
A stack and a queue are both simple ways of storing items in order, but they differ in one rule: a stack removes the last item you added (LIFO β Last In, First Out), while a queue removes the first item you added (FIFO β First In, First Out). A stack behaves like a pile of plates; a queue behaves like a line of people at a ticket counter.
What Is a Stack?
Imagine the plate pile in a kitchen. You place clean plates on top of each other, and when you need one, you take it from the top. You never pull a plate from the middle unless you enjoy washing the floor as well. That is a stack: new items go on top, and removals also happen at the top. The last plate you placed is the first one you take away β Last In, First Out.
A stack supports two main operations. Push adds an item to the top, and pop removes the item from the top.β¦