DEV Community

Moya Richards
Moya Richards

Posted on

1 1

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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay