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