Reputation: 1954
I am making a music streaming application. One of the features is a play queue much like Spotify's where songs will automatically play from the artist, album, or playlist you started from or play songs you have queued if there are any.
I am considering what data structure(s) I will need for this. I am currently weighing the options of using a priority queue, where queued songs have priority over non-queued songs; or two normal queues, an automatic queue and a queued songs queue. I am also open to any better solutions that are out there.
What data structure(s) should I choose?
Upvotes: 3
Views: 5220
Reputation: 55649
Most likely two queues.
With a priority queue (at least a heap) operations take O(log n), where-as it would take O(1) with two queues. Not that it would make a massive difference, unless you have a super performance critical application and there are enough items to actually make a noticeable difference (and the heap will likely come with enough overhead to make it slower for a small n
).
Two queues should also make for a slightly simpler, more understandable implementation, which should be the deciding factor here.
Can't you just have one queue though - for the chosen songs? The next ones if the queue is empty can just be randomly generated, assuming you don't want to display the next few songs that will be playing that aren't in the queue.
A random note - you may want to keep track of songs already played to minimize the generate songs in such a way to eliminate the chance of the same song playing too often - for this you could probably use a hash table.
Upvotes: 4