Priority queue based on a sorted linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • morris11
    New Member
    • Mar 2008
    • 8

    Priority queue based on a sorted linked list

    I want to implement a priority queue based on a sorted linked list. The remove operation on the priority queue should remove the item with a smallest key. Can anybody give me an idea how to do that?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Well...Java has a PriorityQueue class builtin that derives from LinkedList...un less this is an academic excercise, in which case you need to work out how to sort a linked list even remotely efficiently. I recommend a variation on Selection Sort, though swapping is a little more complicated than it would be in an array. Once sorted, the smallest item should be the front node.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by Laharl
      Well...Java has a PriorityQueue class builtin that derives from LinkedList...
      Really? java.util.Prior ityQueue is "An unbounded priority queue based on a priority heap". The heap data structure make much more sense than a linked list. Why use a linked list in the first place?

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Originally posted by BigDaddyLH
        Really? java.util.Prior ityQueue is "An unbounded priority queue based on a priority heap". The heap data structure make much more sense than a linked list. Why use a linked list in the first place?
        That's what I get for going off memory rather than looking at the API...;)

        Comment

        Working...