WTF is are Stacks, Queues, and Deques ?

Stack

A stack is a stack of books, or a stack of sandbags or a stack of elephants or even a stack of unicorns. Basically a stack is anything that follows LIFO (Last In First Out), which means that if the last thing you put onto the stack is the first thing you have to take out, then its a stack.

Lets say you build a tower of blocks, that tower is a stack. Why ? Cause to get to the block at the base you need to take off all the other blocks. Below you will see a visual example of what I am talking about.

You can implement a Stack using a singly linked list.

Queue

A Queue in programming is the same thing as the Queue while in line to buy food, or go to a movie, or get into a night club, its a first come first serve basis. Meaning the first thing to get out of a Queue was the first thing to go into the Queue.

You can also think of Queues as pipes that transport things. In the case of plumbing, the steel pipe and the water moving through it the Queue. Since the water that first enters the pipe is the water that first leaves the pipe.

Below is a diagram to show what I am talking about:

You can implement a Queue that has a O(1) enqueue or dequeue time complexity using a singly linked list.

Deques

Deques are like the cooler older brother of the Queue, since it lets things flow in more then one direction. You can now enqueue or dequeue from the back or the front of the queue, giving the user of the data structure more flexibility. If you understood how Queues worked, then you should be able to understand the diagram below.

You can implement a Deque that has a O(1) enqueue or dequeue time complexity using a doubly linked list.