C#アルゴリズム入門から走路第1章:リニアテーブルの両端キュー
デュアルエンドキュー
EnterFrontキューヘッダfront-OutFront削除キューヘッダエレメントfront++EnterRearキューヘッダrear削除キュー末尾rear++OutRear削除キュー末尾エレメントrear--
EnterFrontキューヘッダfront-OutFront削除キューヘッダエレメントfront++EnterRearキューヘッダrear削除キュー末尾rear++OutRear削除キュー末尾エレメントrear--
///
///
///
///
class CirDeque
{
///
///
///
private const int DEFAULT_SIZE = 10;
///
///
///
private T[] data;
///
///
///
private int front;
///
///
///
private int rear;
///
///
///
///
public bool IsEmpty() => this.rear == this.front;
///
///
///
///
public void Expand()
{
if((this.rear + 1) % this.data.Length == this.front)
{
Array.Resize(ref this.data,
this.data.Length + this.data.Length << 1);
}
}
///
///
///
///
public CirDeque(int size=DEFAULT_SIZE)
{
this.data = new T[size];
this.front = 0;
}
///
///
///
///
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;
}
}
///
///
///
///
public void EnterRear(T val)
{
Expand();
this.data[this.rear] = val;
this.rear = (this.rear + 1) % this.data.Length;
}
///
///
///
///
public T OutFront()
{
if(IsEmpty())
throw new IndexOutOfRangeException(" ");
var val= this.data[this.front];
this.front = (this.front + 1) % this.data.Length;
return val;
}
///
///
///
///
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];
}
}
}