设计循环双端队列
题目描述
public class MyCircularDeque
{
private int capacity;
private int [ ] arr;
private int front;
private int rear;
public MyCircularDeque ( int k)
{
capacity = k + 1 ;
arr = new int [ capacity] ;
front = 0 ;
rear = 0 ;
}
public bool InsertFront ( int value )
{
if ( IsFull ( ) ) return false ;
front = ( front - 1 + capacity) % capacity;
arr[ front] = value ;
return true ;
}
public bool InsertLast ( int value )
{
if ( IsFull ( ) ) return false ;
arr[ rear] = value ;
rear = ( rear + 1 ) % capacity;
return true ;
}
public bool DeleteFront ( )
{
if ( IsEmpty ( ) ) return false ;
front = ( front + 1 ) % capacity;
return true ;
}
public bool DeleteLast ( )
{
if ( IsEmpty ( ) ) return false ;
rear = ( rear - 1 + capacity) % capacity;
return true ;
}
public int GetFront ( )
{
if ( IsEmpty ( ) ) return - 1 ;
return arr[ front] ;
}
public int GetRear ( )
{
if ( IsEmpty ( ) ) return - 1 ;
return arr[ ( rear - 1 + capacity) % capacity] ;
}
public bool IsEmpty ( )
{
return front == rear;
}
public bool IsFull ( )
{
return ( rear + 1 ) % capacity == front;
}
}