// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; namespace E { public class PriorityQueue { private SortedList> q = new SortedList>(); public void Enqueue(int priority, T item) { if(!q.ContainsKey(priority)) { q.Add(priority, new Queue()); } q[priority].Enqueue(item); } public T Dequeue() { int lowpriority = q.Keys[0]; T res = q[lowpriority].Dequeue(); if(q[lowpriority].Count == 0) { q.Remove(lowpriority); } return res; } public bool Empty() { return q.Count == 0; } } public class Test { public static void Main(string[] args) { PriorityQueue q = new PriorityQueue(); q.Enqueue(30, "A"); q.Enqueue(10, "B"); q.Enqueue(20, "C"); while(!q.Empty()) { Console.WriteLine(q.Dequeue()); } } } }