首页 > 代码库 > 05 2 栈与队列 队列的实现及Queue类

05 2 栈与队列 队列的实现及Queue类

技术分享

技术分享

技术分享
 1 class CQueue {
 2 
 3     /// <summary>
 4     /// 存储数据
 5     /// </summary>
 6     private ArrayList m_arrayList;
 7 
 8     public CQueue() {
 9         m_arrayList = new ArrayList();
10     }
11 
12     /// <summary>
13     /// 入队
14     /// </summary>
15     public void EnQueue(object item) {
16         m_arrayList.Add(item);
17     }
18 
19     /// <summary>
20     /// 出队
21     /// </summary>
22     public object DeQueue() {
23         object obj = m_arrayList[0];
24         m_arrayList.RemoveAt(0);
25         return obj;
26     }
27 
28     /// <summary>
29     /// 查看队首元素
30     /// </summary>
31     public object Peek() {
32         return m_arrayList[0];
33     }
34 
35     /// <summary>
36     /// 清除
37     /// </summary>
38     public void Clear() {
39         m_arrayList.Clear();
40     }
41 
42     /// <summary>
43     /// 获取数量
44     /// </summary>
45     public int Count {
46         get {
47             return m_arrayList.Count;
48         }
49     }
50 }
CQueue
技术分享
 1 /// <summary>
 2 /// 优先级项
 3 /// </summary>
 4 public struct PriorityQueueItem {
 5     /// <summary>
 6     /// 优先级
 7     /// </summary>
 8     public int m_Priority;
 9     /// <summary>
10     /// 名称
11     /// </summary>
12     public string m_Name;
13 }
14 
15 /// <summary>
16 /// 极简优先级队列
17 /// </summary>
18 public class PriorityQueue:Queue {
19 
20     public PriorityQueue() { }
21 
22     public override object Dequeue() {
23 
24         object[] items;
25         int min;
26         //获取包含队列所有元素的新数组
27         items = this.ToArray();
28 
29         //获取这个数组中的最小优先级
30         min = ((PriorityQueueItem)items[0]).m_Priority;
31         for(int x = 1;x <= items.GetUpperBound(0);x++) {
32             if(((PriorityQueueItem)items[x]).m_Priority < min) {
33                 min = ((PriorityQueueItem)items[x]).m_Priority;
34             }
35         }
36 
37         //清空队列
38         this.Clear();
39 
40         int x2;
41         //遍历新数组
42         for(x2 = 0;x2 <= items.GetUpperBound(0);x2++) {
43             //只将最低优先级项入队列
44             if(((PriorityQueueItem)items[x2]).m_Priority == min && ((PriorityQueueItem)items[x2]).m_Name != "") {
45                 this.Enqueue(items[x2]);
46             }
47         }
48         //出队列
49         return base.Dequeue();
50     }
51 }
极简优先级队列

 

05 2 栈与队列 队列的实现及Queue类