Guest User

Untitled

a guest
Oct 12th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. public class PriorityQueue
  2. {
  3.     // I'm using an unsorted array for this example, but ideally this
  4.     // would be a binary heap. Find a binary heap class:
  5.     // * https://bitbucket.org/BlueRaja/high-speed-priority-queue-for-c/wiki/Home
  6.     // * http://visualstudiomagazine.com/articles/2012/11/01/priority-queues-with-c.aspx
  7.     // * http://xfleury.github.io/graphsearch.html
  8.     // * http://stackoverflow.com/questions/102398/priority-queue-in-net
  9.     private List<NodeElement> elements = new List<NodeElement>();
  10.     public List<NodeElement> elementstoremove = new List<NodeElement>();
  11.  
  12.     public int Count
  13.     {
  14.         get { return elements.Count; }
  15.     }
  16.  
  17.     public void Enqueue(NodeElement next, double priority)
  18.     {
  19.         next.passed = true;
  20.         next.priority = priority;
  21.         elements.Add(next);
  22.         elementstoremove.Add(next);
  23.     }
  24.     public NodeElement Dequeue()
  25.     {
  26.         int bestIndex = 0;
  27.         for (int i = 0; i < elements.Count; i++)
  28.         {
  29.             if (elements[i].priority < elements[bestIndex].priority)
  30.             {
  31.                 bestIndex = i;
  32.             }
  33.         }
  34.         NodeElement bestItem = elements[bestIndex];
  35.         elements.RemoveAt(bestIndex);
  36.         return bestItem;
  37.     }
  38.  
  39. }
Add Comment
Please, Sign In to add comment