1。定義隊列類
using System;
namespace Stack
{
/// <summary>
/// 隊列數據結構
/// </summary>
public class Queue
{
public Queue(){}
private int count=0;//隊列中的節點個數
private Node front=null;//輸出端
private Node rear=null;//輸入端
/// <summary>
/// 隊列中節點總數
/// </summary>
public int Count
{
get{return count;}
}
/// <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)
{
throw new Exception("隊列空!");
}
else
{
object objValue = front.Value;
front = front.NodeNext;
count--;
return objValue;
}
}
[隊列結點類]
}
}
2。使用隊列
private Queue objQ = new Queue();
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=0;i<100;i++)
{
objQ.Push(i);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
for(int i=0;i<objQ.Count;i++)
{
richTextBox1.AppendText(objQ.Pop().ToString() + "\r\n");
}
}
2。使用隊列
浙公网安备 33010602011771号