Learn how software decides which item should be handled next.
Stacks and queues are abstract data types for organising items. A stack removes the newest item first. A queue removes the oldest item first. These rules are called LIFO and FIFO, and they appear throughout real software.
This stack grows upward as you push items onto its top. The newest item, C, is removed first. Then B is removed, followed by A. That is last in, first out, or LIFO.
This queue has an entry point and an exit point. New items join at the rear, while items leave from the front. A arrives before B and C, so A must leave first. That is first in, first out, or FIFO.
A stack needs push and pop, both at the top. A queue needs enqueue at the rear and dequeue at the front. With a suitable implementation, each operation can run in constant time, written as big O of one.
Here, the list represents a stack. Append adds an item to the end, which is the top. Pop removes and returns that same final item. The last two lines therefore output C, then B.
Use a stack when the newest unfinished task should return first, such as undo actions or function calls. Use a queue when fairness or arrival order matters, such as print jobs. A common mistake is removing from the wrong end of a queue.
When a browser goes back, it can pop the most recent page from a history stack. A printer usually processes jobs from a queue, in arrival order. Breadth-first search also uses a queue. Choosing the wrong structure can change the algorithm's result.