3

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?

the
  • 21,007
  • 11
  • 68
  • 101
joshreesjones
  • 1,934
  • 5
  • 24
  • 42
  • 1
    +1 because this shows a lot more of an attempt at solving the problem at hand than many questions that get quite a few upvotes and no downvotes. I'm guessing the reason it was downvoted is because you could've researched the applicable data structures to get an idea of the running time and programmatic complexity and (at least tried to) have come to the conclusion of which is better yourself, but you did consider two solutions, so yeah, I don't know. Perhaps it came across as too opinion-based. – Bernhard Barker Mar 23 '14 at 21:07

1 Answers1

4

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.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • "Can't you just have one queue though?" - I do want to have songs after queued songs to appear. Thanks, though! This was a helpful answer. – joshreesjones Mar 24 '14 at 00:08