DEV Community

Moya Richards
Moya Richards

Posted on

Simplifying: Stacks and Queues

Simplifying:  Stacks and Queues

Stacks and queues: this is how I remember them:

Stacks: I picture something vertical: a pile of plates,a bottle.

Queues: I picture something horizontal: a pipe, a line (I join first, I get served first).

Stacks - LIFO:  you can only add(append, push) and remove(pop) from the back(top,end).
Queues - FIFO : add (enqueue,append) to the back(rear), only remove(dequeue,popleft) from the front .
Enter fullscreen mode Exit fullscreen mode

LIFO : Last in, first out.
FIFO : first in, first out.

If you are JavaScript developer, you are unconsciously working with stacks and queues everyday:

  • You use stacks every time you run your code: "function call stack".
  • You use queues every time you run asynchronous code: "The event queue" of the event loop.

Here are some examples of stacks and queues in the real world:

Stack:

  • Your favorite text editor: undo/redo feature.
  • Backtracking: your browsers "back" button.
  • Reverse : try to reverse your name.

Queue:

  • Order processing: you stand 6 feet apart from everyone as you wait in line to place your order with a cashier.
  • Message processing: your long SMS messages are stored in a queue( messages are sent in the order they are received). Test this feature out on twitter by exceeding your 143 character limit

Now, how have you used stacks and queues consciously in your career?

Let's talk about your usage of these data structures or concepts in your projects.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)