微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

    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();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐