天天看點

資料結構---隊列(C#)

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

}

}

繼續閱讀