Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
A Priority Queue Implementation in C#
By Jim Mischel

Rate This Article: Add This Article To:

A Priority Queue Implementation in C# - ' Using the class'
( Page 3 of 3 )

Using the PriorityQueue Class

Below is a small test program that creates a priority queue for alarm system events and inserts some items, then removes the items in priority order by successively calling Dequeue until the queue is empty.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Mischel.Collections;

namespace pqTest
{
    enum AlarmEventType
    {
        Test,
        Trouble,
        Alert,
        Fire,
        Panic
    };

    class AlarmEvent
    {
        private AlarmEventType etype;
        private string msg;
        public AlarmEvent(AlarmEventType type, string message)
        {
            etype = type;
            msg = message;
        }

        public AlarmEventType EventType
        {
            get { return etype; }
        }

        public string Message
        {
            get { return msg; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            PriorityQueue<AlarmEvent, AlarmEventType> pq = 
                new PriorityQueue<AlarmEvent, AlarmEventType>();

            // Add a bunch of events to the queue
            pq.Enqueue(new AlarmEvent(AlarmEventType.Test, 
                "Testing 1"), AlarmEventType.Test);
            pq.Enqueue(new AlarmEvent(AlarmEventType.Fire, 
                "Fire alarm 1"), AlarmEventType.Fire);
            pq.Enqueue(new AlarmEvent(AlarmEventType.Trouble, 
                "Battery low"), AlarmEventType.Trouble);
            pq.Enqueue(new AlarmEvent(AlarmEventType.Panic, 
                "I've fallen and I can't get up!"), 
                AlarmEventType.Panic);
            pq.Enqueue(new AlarmEvent(AlarmEventType.Test, 
                "Another test."), AlarmEventType.Test);
            pq.Enqueue(new AlarmEvent(AlarmEventType.Alert, 
                "Oops, I forgot the reset code."), 
                AlarmEventType.Alert);

            Console.WriteLine(
                "The queue contains {0} events", pq.Count);

            // Now remove the items in priority order
            Console.WriteLine();
            while (pq.Count > 0)
            {
                PriorityQueueItem<AlarmEvent, 
                    AlarmEventType> item = pq.Dequeue();
                Console.WriteLine("{0}: {1}", item.Priority,
                    item.Value.Message);
            }
        }
    }
}

The full source code for the PriorityQueue class and the test program is available for download from http://www.mischel.com/pubs/priqueue.zip. You are free to use the code in any manner you like.



 
 
>>> More Microsoft Languages Articles          >>> More By Jim Mischel