- Published on
Queues - JavaScript Data Structures
432 words3 min read
- Authors
- Name
- Curtis Warcup
Definition
Queues are a data type or collection in which the collection maintains a particular oder in which the elements are added and removed. Queues follow First-In-First-Out (FIFO) principle. The first element added to the queue is the first one to be removed.
We should be able to... | Run This |
---|---|
Create a new, empty queue | const queue = new Queue(); |
Add an element to the end of the queue | queue.add(1); |
Remove the element from the front of the queue | queue.remove(); |
Check if a queue is empty | isEmpty() |
Implementation
class Queue {
constructor() {
this.data = []
}
enqueue(item) {
this.data.push(item)
}
dequeue(item) {
return this.data.shift()
}
peek(item) {
return this.data[0]
}
isEmpty() {
return this.data.length === 0
}
}
// Test
const queue = new Queue()
queue.isEmpty() // true
queue.enqueue('A')
queue.enqueue('B')
queue.enqueue('C')
queue.enqueue('D')
queue.enqueue('E')
queue.isEmpty() // false
queue.peek() // 'A'
queue.dequeue() // 'A'
queue.dequeue() // 'B'
queue.dequeue() // 'C'
- Create a
class
with aconstructor
that initializes an empty array ofdata
. - Define an
enqueue()
method to add an element to the end of thedata
array. - Define a
dequeue()
method to remove an element from the start of thedata
array. - Define a
peek()
method, which retrieves the value of the first element in thedata
array, without removing it. - Define an
isEmpty()
method to determine if thedata
array is empty.