Priority Queues with C#: Listing 3.

Adding an item to a priority queue.

public void Enqueue(T item)
{
  data.Add(item);
  int ci = data.Count - 1;
  while (ci  > 0)
  {
    int pi = (ci - 1) / 2;
    if (data[ci].CompareTo(data[pi])  >= 0)
      break;
    T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp;
    ci = pi;
  }
}

About the Author

Dr. James McCaffrey works for Microsoft Research in Redmond, Wash. He has worked on several Microsoft products including Azure and Bing. James can be reached at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube