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

  • Steve Sanderson Wows Web-Devs with Peek at 'Blazor United' for .NET 8

    "We've started some experiments to combine the advantages of Razor Pages, Blazor Server and Blazor WebAssembly all into one thing."

  • Spring Cloud Azure 5.0 Ships with Updated, Redesigned Documentation

    "We've created a new online resource, Azure for Spring developers, to help Spring developers code, deploy and scale their Spring applications on Azure.

  • What's New in Progress Telerik UI for Blazor, .NET MAUI and WinForms

    The company said its new Progress Developer Tools R1 2023 release includes design and accessibility upgrades, deeper customizations and support for the latest frameworks.

  • Take ChatGPT for a Spin with VS Code Tools

    With ChatGPT being the first "It" tech in the cutting-edge AI space that regular people can play around with, it's no wonder that tools to use it are exploding in the Visual Studio Code Marketplace.