C#——實現泛型順序佇列和鏈式佇列
阿新 • • 發佈:2019-01-09
順序佇列,使用List為儲存器
/// 順序佇列
class MyQueue<T>
{
List<T> list;
public MyQueue()
{
list = new List<T>();
}
//入隊
public void EnQueue(T item)
{
list.Add(item);
}
//出隊
public void DeQueue ()
{
if(list.Count>0)
list.RemoveAt(0);
}
}
鏈佇列,以結點相連的單鏈表(只能隊首刪除,隊尾插入)
class LinkQueue<T>
{
private LinkNode<T> rear;
private LinkNode<T> front;
private int nodeCount;
public LinkQueue()
{
rear = null ;
front = null;
nodeCount = 0;
}
//入隊
public void Enqueue(T value)
{
LinkNode<T> p = new LinkNode<T>(value);
if ((rear == null)&&(front==null))
{
front = p;
rear = p;
}
else
{
front.Next = p;
front = front.Next;
}
nodeCount++;
}
//出隊
public void Dequeue()
{
if (front != null)
{
rear = rear.Next;
nodeCount--;
}
}
public void ShowAllItem()
{
LinkNode<T> p = rear;
while (p!=null)
{
Console.WriteLine(p.Data);
p = p.Next;
}
Console.WriteLine("----------------");
}
public int Count
{
get { return nodeCount; }
}
}