12.链队列
namespace DSList
{
public class LinkQueue<T> : IQueue<T>
{
//Fields
private Node<T> front;
private Node<T> rear;
private int count;
//Properties
public Node<T> Front
{
get
{
return front;
}
set
{
front = value;
}
}
public Node<T> Rear
{
get
{
return rear;
}
set
{
rear = value;
}
}
public int Count
{
get
{
return count;
}
set
{
count = value;
}
}
//Constructor
public LinkQueue()
{
front = null;
rear = null;
count = 0;
}
//Base methods
public int GetLength()
{
return count;
}
public void Clear()
{
front = null;
rear = null;
count = 0;
}
public bool IsEmpty()
{
return front == null && count == 0;
}
public void In(T item)
{
Node<T> newNode = new Node<T>(item);
if (IsEmpty())
{
front = rear = newNode;
}
else
{
rear.Next = newNode;
rear = rear.Next;
}
++count;
}
public T Out()
{
if (IsEmpty())
{
Console.WriteLine("LinkQueue is empty!");
return default(T);
}
Node<T> tmpNode = front;
front = front.Next;
--count;
return tmpNode.Data;
}
public T GetFront()
{
if (IsEmpty())
{
Console.WriteLine("LinkQueue is empty!");
return default(T);
}
return front.Data;
}
public void Output()
{
Node<T> p = front;
while (p != null)
{
Console.Write(p.Data + " ");
p = p.Next;
}
Console.WriteLine();
}
}
}