[数据结构与算法]队列的优先级

    public struct Pqitem
    {
        public int priority;
        public string name;
    }
    class CQueue
    {
        private ArrayList pqueue; 
        public CQueue() 
        {
            pqueue = new ArrayList();
        }
        public void EnQueue(object item) 
        {
            pqueue.Add(item);
        }
        public object DeQueue(bool isok) 
        {
            //pqueue.RemoveAt(0);
            //优先级
            if (isok)
            {
                object[] items;
                int min;

                items = pqueue.ToArray();
                min = ((Pqitem)items[0]).priority;
                //获取最小优先级
                for (int i = 0; i <= items.GetUpperBound(0); i++)
                {
                    if (((Pqitem)items[i]).priority < min)
                    {
                        min = ((Pqitem)items[i]).priority;
                    }

                }
                pqueue.Clear();
                //最小优先级进队列
                int x;
                for (x = 0; x <= items.GetUpperBound(0); x++)
                {
                    if (((Pqitem)items[x]).priority == min && ((Pqitem)items[x]).name != "")
                    {
                        pqueue.Add(items[x]);
                    }
                }
            }
            object obj = pqueue[0];
            pqueue.RemoveAt(0);
            return obj;
        }
        public object Peek() 
        {
            return pqueue[0];
        }
        public void ClearQueue() 
        {
            pqueue.Clear();
        }
        public int Count() 
        {
            return pqueue.Count;
        }

    }
        //构造的队列实现的,有优先级的
        CQueue erwait = new CQueue();
        Pqitem[] erpatient = new Pqitem[3];
        Pqitem nexpatient;
        erpatient[0].name = "XiaoMing";
        erpatient[0].priority = 1;
        erpatient[1].name = "DaMao";
        erpatient[1].priority = 0;
        erpatient[2].name = "ErMao";
        erpatient[2].priority = 3;
        for (int i = 0; i <= erpatient.GetUpperBound(0); i++)
            erwait.EnQueue(erpatient[i]);
        nexpatient = (Pqitem)erwait.DeQueue(true);
        Console.WriteLine(nexpatient.name);

        Console.Read();

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...