双端队列
EnterFront将元素插到队列头部 front- -
OutFront删除队列头部元素 front++
EnterRear将元素插到到队列尾部 rear++
OutRear删除队列尾部元素 rear- -
/// <summary>
/// 循环双端队列
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
class CirDeque<T>
{
/// <summary>
/// 默认队列大小
/// </summary>
private const int DEFAULT_SIZE = 10;
/// <summary>
/// 数据域
/// </summary>
private T[] data;
/// <summary>
/// 队列头
/// </summary>
private int front;
/// <summary>
/// 队列尾巴
/// </summary>
private int rear;
/// <summary>
/// 判断队列满
/// </summary>
/// <returns>返回是否满</returns>
public bool IsEmpty() => this.rear == this.front;
/// <summary>
/// 判断队列满
/// </summary>
/// <returns></returns>
public void Expand()
{
if((this.rear + 1) % this.data.Length == this.front)
{
Array.Resize(ref this.data,
this.data.Length + this.data.Length << 1);
}
}
/// <summary>
/// 构造器
/// </summary>
/// <param name="size">大小</param>
public CirDeque(int size=DEFAULT_SIZE)
{
this.data = new T[size];
this.front = 0;
}
/// <summary>
/// 队头进队
/// </summary>
/// <param name="val">数据元素</param>
public void EnterFront(T val)
{
Expand();
if(this.front==0)
{
this.front = this.data.Length - 1;
this.data[this.front] = val;
}
else
{
this.front = (this.front - 1) % this.data.Length;
this.data[this.front] = val;
}
}
/// <summary>
/// 队尾进队
/// </summary>
/// <param name="val">数据元素</param>
public void EnterRear(T val)
{
Expand();
this.data[this.rear] = val;
this.rear = (this.rear + 1) % this.data.Length;
}
/// <summary>
/// 队头出队
/// </summary>
/// <param name="val">数据元素</param>
public T OutFront()
{
if(IsEmpty())
throw new IndexOutOfRangeException("队空");
var val= this.data[this.front];
this.front = (this.front + 1) % this.data.Length;
return val;
}
/// <summary>
/// 队尾出队
/// </summary>
/// <param name="val">数据元素</param>
public T OutRear()
{
if (IsEmpty())
throw new IndexOutOfRangeException("队空");
if(this.rear==0)
{
this.rear = this.data.Length - 1;
return this.data[this.rear];
}
else
{
this.rear = (this.rear - 1) % this.data.Length;
return this.data[this.rear];
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。