代码
//接口
public interface IQueue<T>
{
int GetLength(); //求队列的长度
bool IsEmpty(); //判断对列是否为空
void Clear(); //清空队列
void In(T item); //入队
T Out(); //出队
T GetFront(); //取对头元素
}
//接口的实现
public class LinkQueue<T> : IQueue<T>
{
private Node<T> front; //队列头指示器
private Node<T> rear; //队列尾指示器
private int num; //队列结点个数
//队头属性
public Node<T> Front
{
get
{
return front;
}
set
{
front = value;
}
}
//队尾属性
public Node<T> Rear
{
get
{
return rear;
}
set
{
rear = value;
}
}
//队列结点个数属性
public int Num
{
get
{
return num;
}
set
{
num = value;
}
}
//构造器
public LinkQueue()
{
front = rear = null;
num = 0;
}
//求链队列的长度
public int GetLength()
{
return num;
}
//清空链队列
public void Clear()
{
front = rear = null;
num = 0;
}
//判断链队列是否为空
public bool IsEmpty()
{
if ((front == rear) && (num == 0))
{
return true;
}
else
{
return false;
}
}
//入队
public void In(T item)
{
Node<T> q = new Node<T>(item);
if (rear == null)
{
rear = q;
}
else
{
rear.Next = q;
rear = q;
}
++num;
}
//出队
public T Out()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
Node<T> p = front;
front = front.Next;
if (front == null)
{
rear = null;
}
--num;
return p.Data;
}
//获取链队列头结点的值
public T GetFront()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
return front.Data;
}
}
public interface IQueue<T>
{
int GetLength(); //求队列的长度
bool IsEmpty(); //判断对列是否为空
void Clear(); //清空队列
void In(T item); //入队
T Out(); //出队
T GetFront(); //取对头元素
}
//接口的实现
public class LinkQueue<T> : IQueue<T>
{
private Node<T> front; //队列头指示器
private Node<T> rear; //队列尾指示器
private int num; //队列结点个数
//队头属性
public Node<T> Front
{
get
{
return front;
}
set
{
front = value;
}
}
//队尾属性
public Node<T> Rear
{
get
{
return rear;
}
set
{
rear = value;
}
}
//队列结点个数属性
public int Num
{
get
{
return num;
}
set
{
num = value;
}
}
//构造器
public LinkQueue()
{
front = rear = null;
num = 0;
}
//求链队列的长度
public int GetLength()
{
return num;
}
//清空链队列
public void Clear()
{
front = rear = null;
num = 0;
}
//判断链队列是否为空
public bool IsEmpty()
{
if ((front == rear) && (num == 0))
{
return true;
}
else
{
return false;
}
}
//入队
public void In(T item)
{
Node<T> q = new Node<T>(item);
if (rear == null)
{
rear = q;
}
else
{
rear.Next = q;
rear = q;
}
++num;
}
//出队
public T Out()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
Node<T> p = front;
front = front.Next;
if (front == null)
{
rear = null;
}
--num;
return p.Data;
}
//获取链队列头结点的值
public T GetFront()
{
if (IsEmpty())
{
Console.WriteLine("Queue is empty!");
return default(T);
}
return front.Data;
}
}