Professional Documents
Culture Documents
21, 21:04
Greetings,
I enjoy reading the articles that you write :).
I have implemented the solution for a queue with an error here but, I am not sure about how the Time
Complexity is affected by a comparison.
Also, can Array be used to provide a queue like functionality? If yes, will the time complexity for
accessing the Head and Remove an item be O(1)?
Regards
SNikhill
Greetings,
I enjoy reading the articles that you write :).
I have implemented the solution for a queue with an error here but, I am not sure about how the
Time Complexity is affected by a comparison.
Also, can Array be used to provide a queue like functionality? If yes, will the time complexity for
accessing the Head and Remove an item be O(1)?
The problem with using an array is that the queue.dequeue() operation is going to be implemented
using array.shift() - which has O(n) time complexity.
https://utteranc.es/utterances.html?src=https%3A%2F%2Futtera…g%3Atitle=How%20to%20Implement%20a%20Queue%20in%20JavaScript Page 1 of 3
utterances 30.03.21, 21:04
this.items[this.tailIndex] = item;
Using an object property to enqueue items to the queue is a really clever way to allow the dequeue
operation to be in constant time O(1).
delete this.items[this.headIndex];
this.items[this.tailIndex] = item;
Using an object property to enqueue items to the queue is a really clever way to allow the
dequeue operation to be in constant time O(1).
delete this.items[this.headIndex];
Thanks Nicolas! Yes, using a plain object allows keeping the queue operations under O(1) time.
This is a nice and good example to understand queue. Thanks for writing this one Dmitri Pavlutin
This is a nice and good example to understand queue. Thanks for writing this one Dmitri Pavlutin
https://utteranc.es/utterances.html?src=https%3A%2F%2Futtera…g%3Atitle=How%20to%20Implement%20a%20Queue%20in%20JavaScript Page 2 of 3
utterances 30.03.21, 21:04
Write Preview
Sign in to comment
https://utteranc.es/utterances.html?src=https%3A%2F%2Futtera…g%3Atitle=How%20to%20Implement%20a%20Queue%20in%20JavaScript Page 3 of 3