You are on page 1of 3

utterances 30.03.

21, 21:04

6 Comments - powered by utteranc.es

SNikhill commented a week ago

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

panzerdp commented a week ago Owner

Greetings,
I enjoy reading the articles that you write :).

Hey @SNikhill. Thanks!

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.

I have checked your solution and it looks great to me.

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

NicholasMurray commented 6 days ago

Awesome implementation of a queue.

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];

panzerdp commented 6 days ago Owner

Awesome implementation of a queue.

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.

If you're interested, take a look at the performance of different queue implementations:


https://github.com/tracker1/queue-testing

vipinc007 commented 9 hours ago

This is a nice and good example to understand queue. Thanks for writing this one Dmitri Pavlutin

panzerdp commented 8 hours ago Owner

This is a nice and good example to understand queue. Thanks for writing this one Dmitri Pavlutin

Glad you like it @vipinc007!

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

Styling with Markdown is supported Sign in with GitHub

https://utteranc.es/utterances.html?src=https%3A%2F%2Futtera…g%3Atitle=How%20to%20Implement%20a%20Queue%20in%20JavaScript Page 3 of 3

You might also like