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 .
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.
I coded a Java paint application to draw shapes on a canvas : https://github.com/moyarich/JPaint.
I used the open source bull queue manager in my project to control the pace at which I send data to a API. Each subsequent item is sent to the API, after it connects to my webhook url. I wrote this custom function(https://gist.github.com/moyarich/4d6735b8d417c5e2f7e5f03469d32fb7)to get bull queue to manually process only one job in the queue on demand.
Top comments (0)