原创

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/52585049
    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();
文章最后发布于: 2016-09-19 13:38:38
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览