您当前的位置: 首页 >  数据结构与算法

暂无认证

  • 0浏览

    0关注

    96099博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

发布时间:2016-09-19 13:38:38 ,浏览量:0

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();
关注
打赏
1655516835
查看更多评论
立即登录/注册

微信扫码登录

0.0751s