Ever wondered what Stacks and Queues mean? I've written a simple explanations and commonly used analogies to help briefly explain what they mean.
Stacks are linear data structures meaning elements can be inserted or deleted from only one side of the list. Stacks implement LIFO (last-in, first-out), meaning the last element that was added to the list is the first one that will be taken out. Think of PEZ candy, When you stack the candies in the little slot, the last one you inserted will be the first one you take out and eat.
The INSERT operation for a linear stack structure is PUSH and DELETE is POP. You may be familiar with these methods when dealing with arrays. Using query operations, you can see whether a stack is empty STACK-EMPTY, attempting to pop an empty stack will result in an underflows error.
Queues are similar to Stacks except they implement FIFO (first-in, first-out). INSERT operations on queues are called ENQUEUE and DELETE operation is called DEQUEUE.
A common analogy used to depict an example of how queues work is restaurants. Think of a restaurant line, you want to help the customer that was there first not the one that arrived last. Queues have a head and a tail, when an element is enqueued, it takes place of the tail and the similar to the recently arrived customer. The dequeued takes place of the head element, as if you just helped that customer that's waited the longest.
I hope you've enjoyed my blog, please if you think i should add more let me know!
Have a wonderful day/night!
Top comments (0)