Queue.cs
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface
{
/// <summary>
/// 隊列數據結構
/// </summary>
public class Queue
{
public Queue(){}
private int count=0;//隊列中的節點個數
private bool isNull=false;// 隊列是否空
private Node front=null;//輸出端
private Node rear=null;//輸入端
/**//// <summary>
/// 隊列中節點總數
/// </summary>
public int Count
{
get{return count;}
}
/**/
/// <summary>
/// 隊列是否空
/// </summary>
public bool IsNull
{
get { return isNull; }
}
/**//// <summary>
/// 進隊
/// </summary>
/// <param name="o">進隊對象</param>
public void Push(object o)
{
Node nextNode = new Node(o);
if(rear!=null)rear.NodeNext = nextNode;
rear = nextNode;
if(front==null) front=rear;
count++;
}
/**//// <summary>
/// 出隊
/// </summary>
/// <returns>隊列當前節點值</returns>
public object Pop()
{
if(front==null)
{
isNull=true;
return "";
}
else
{
object objValue = front.Value;
front = front.NodeNext;
count--;
return objValue;
}
}
#region [隊列結點類]
/**//// <summary>
/// 隊列結點類
/// </summary>
private class Node
{
private Node _node;
private object _value;
public Node(object v)
{
this._value = v;
this._node = null;
}
public Node NodeNext
{
get{return _node;}
set{_node = value;}
}
public object Value
{
get{return _value;}
}
}
#endregion
}
}