您的位置:首页 > 理论基础 > 数据结构算法

数据结构---队列(C#)

2010-04-09 14:04 190 查看
namespace DateStructrue
{

public interface IQueue<T> : IDateStructrue
{
void EnQueue(T item);
T DeQueue();
T GetFront();
//T this[int index] { get; set; }
bool IsFull { get; }
}
}


namespace DateStructrue
{
public class Queue<T>:IQueue<T>
{
private T[] _item;
private int _head;
private int _tail;

public Queue() : this(10) { }

public Queue(int size)
{
_item = new T[size];
}

#region IQueue<T> Members

public void EnQueue(T item)
{
if (IsFull)
{
T[] destinationArray = new T[this._item.Length + 10];
if (this._head < this._tail)
{ Array.Copy(this._item, this._head, destinationArray, 0, this.Count); }
else
{ Array.Copy(this._item, 0, destinationArray, 0, this._item.Length); }

this._item = destinationArray;
}
this._item[_tail++] = item;
}

public T DeQueue()
{
T temp = default(T);

if (IsEmpty){ return temp; }

return temp = this._item[_head++];
}

public T GetFront()
{
T temp = default(T);
if (IsEmpty) { return temp; }
return temp = this._item[_head];
}

public bool IsFull
{
get { return this._tail % this._item.Length == 0; }
}

#endregion

#region IDateStructrue Members

public int Count
{
get { return this._tail - this._head; }
}

public void Clear()
{
this._head = this._tail = 0;
}

public bool IsEmpty
{
get { return this._head == this._tail; }
}

#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: