HomeAbout Me

Queue & Stack

By Daniel Nguyen
Published in Algorithm
April 26, 2024
1 min read
Queue & Stack

Queue

example
example

First in first out (FIFO) data structure: first element added will be processed first

Operations:

  • Insert (enqueue): new element is added at the end of the queue: O(1)
  • Remove (dequeue): you only can remove the first element: O(1)

Most languages support built-in Queue implementation. Just use the built-in function in the interview; no need to reimplement it unless the interviewer asks.

let arr = []
arr.push("a")
arr.push("b")
arr.push("c")
arr.push("d")
console.log(arr)
//(4) ['a', 'b', 'c', 'd']
arr.shift()
console.log(arr)
//(3) ['b', 'c', 'd']

Queue Application

  • Breadth First Search (BFS): find shortest path in an unweighted graph

  • In reality:

    • message queue system (Kafka, RabbitMQ);
    • waiting lists for a single shared resource (CPU, Disk ..)

Circular Queue

  • Last position is connected to the first position

  • Benefit: reuse space in front of queue

example
example

Double Ended Queue

  • elements can be added to or removed from either the front (head) or back (tail)

example
example

Stack

Last in First out (LIFO)

Operations:

- push(value): push new value to the back of the stack
- pop(): remove the value from the back of the stack

example
example

let arr = []
arr.push("a")
arr.push("b")
arr.push("c")
arr.push("d")
console.log(arr)
arr.pop()
console.log(arr)

Tags

#Algorithm

Share

Previous Article
How `this` behaves in JavaScript
Next Article
Hash Table

Table Of Contents

1
Queue
2
Stack

Related Posts

Hash Table
April 26, 2024
1 min
© 2025, All Rights Reserved.
Powered By

Quick Links

About Me

Legal Stuff

Social Media