c#实现队列(Queue)
1 public class Node
2 {
3 private Node _node;
4 private object _value;
5
6 public Node(object v)
7 {
8 this._value = v;
9 this._node = null;
10 }
11
12 public Node NodeNext
13 {
14 get { return _node; }
15 set { _node = value; }
16 }
17 public object Value
18 {
19 get { return _value; }
20 }
21
22 }
2 {
3 private Node _node;
4 private object _value;
5
6 public Node(object v)
7 {
8 this._value = v;
9 this._node = null;
10 }
11
12 public Node NodeNext
13 {
14 get { return _node; }
15 set { _node = value; }
16 }
17 public object Value
18 {
19 get { return _value; }
20 }
21
22 }
1 public class Queue
2 {
3 public Queue() { }
4 private int count=0;//队列中节点的个数
5 private Node nOutNode=null;//输出端
6 private Node oInNode=null;//输入端
7
8 //// <summary>
9 /// 队列中节点总数
10 /// </summary>
11 public int Count
12 {
13 get{ return count;}
14 }
15
16 /// <summary>
17 /// 入队列
18 /// </summary>
19 /// <param name="o">进队对象</param>
20 public void Push(object o)
21 {
22 Node nextNode = new Node(o);
23 if (oInNode != null)
24 {
25 oInNode.NodeNext = nextNode;
26 }
27
28 oInNode = nextNode;
29
30 if (nOutNode == null)
31 {
32 nOutNode = oInNode;
33 }
34 count++;
35 }
36 /// <summary>
37 /// 出队
38 /// </summary>
39 /// <returns>队列当前节点的值</returns>
40 public object Pop()
41 {
42 if(nOutNode==null)
43 {
44 throw new Exception("队列空!");
45 }
46 else
47 {
48 object objValue = nOutNode.Value;
49 nOutNode = nOutNode.NodeNext;
50 count--;
51 return objValue;
52 }
53 }
54
55
56 }
2 {
3 public Queue() { }
4 private int count=0;//队列中节点的个数
5 private Node nOutNode=null;//输出端
6 private Node oInNode=null;//输入端
7
8 //// <summary>
9 /// 队列中节点总数
10 /// </summary>
11 public int Count
12 {
13 get{ return count;}
14 }
15
16 /// <summary>
17 /// 入队列
18 /// </summary>
19 /// <param name="o">进队对象</param>
20 public void Push(object o)
21 {
22 Node nextNode = new Node(o);
23 if (oInNode != null)
24 {
25 oInNode.NodeNext = nextNode;
26 }
27
28 oInNode = nextNode;
29
30 if (nOutNode == null)
31 {
32 nOutNode = oInNode;
33 }
34 count++;
35 }
36 /// <summary>
37 /// 出队
38 /// </summary>
39 /// <returns>队列当前节点的值</returns>
40 public object Pop()
41 {
42 if(nOutNode==null)
43 {
44 throw new Exception("队列空!");
45 }
46 else
47 {
48 object objValue = nOutNode.Value;
49 nOutNode = nOutNode.NodeNext;
50 count--;
51 return objValue;
52 }
53 }
54
55
56 }
源代码:/Files/HeroBeast/Queue.rar