首页 / 软件开发 / C# / 数据结构(C#):队列
数据结构(C#):队列2011-08-16 博客园 飘遥(Zhenxing Zhou)队列的特点是先进先出,如同日常生活中的排队。队列有加入队尾,从队头删除元素,取得队尾元素 ,取得队头元素,取得队列长度,判断队列是否为空等操作。队列也可以可以用顺序表、链表实现,但队列最好不要用顺序表实现,因为元素加入队列和删除元素 中的一种操作总会引起全部元素的移动,效率极低(循环队列除外)。队列的实现非常简单,下面用前面介绍的单链表实现。代码:/*
* File : Queue.cs
* Author : Zhenxing Zhou
* Date : 2008-12-07
* Blog : http://www.xianfen.net/
*/
namespace Xianfen.Net.DataStructure
{
public class Queue<T>
{
protected SingleLinkedList<T> m_List;
public bool IsEmpty
{
get { return m_List.IsEmpty; }
}
public int Count
{
get { return m_List.Count; }
}
public Queue()
{
m_List = new SingleLinkedList<T>();
}
public Queue(T t)
{
m_List = new SingleLinkedList<T>(t);
}
public T DeQueue()
{
T t = m_List.GetTail();
m_List.RemoveTail();
return t;
}
public void EnQueue(T t)
{
m_List.AddHead(t);
}
public T GetFront()
{
return m_List.GetTail();
}
public T GetRear()
{
return m_List.GetHead();
}
}
}