A queue is a special linear table. It can only be deleted at the front end of the table, but inserted at the back end of the table. The end of the insert operation is called the end of the team, and the end of the delete operation is called the head of the team. This is the first-in-first-out rule (FIFO) that we often use in China. It has been used for a long time before, for example, granary management officials, before mastering this rule, the grain at the bottom of the warehouse broke down for too long. Later, some smart people opened a door on the two sides of the granary and went out of the warehouse while entering the warehouse, this makes management much easier. When there are no elements in a queue, it is called an empty queue.
The queue implementation interface is as follows:
Public interface IQueen <T>
{
Int Length ();
Bool IsEmpty ();
Bool IsFull ();
Void Clear ();
Void IN (T items );
T Out ();
T GetFrontItem ();
} The queue implementation principle and Code are as follows: Code
Public class JQueen <T>: IQueen <T>
{
Private int size;
Private T [] item;
Private int front;
Private int back;
Public JQueen ()
: This (100)
{
Size = 100;
Item = new T [2, 100];
Front = back =-1;
}
Public JQueen (int length)
{
Size = length;
Item = new T [length];
Front = back =-1;
}
Public T this [int index]
{
Get {return item [index];}
Set {item [index] = value ;}
}
Public int Front
{
Get {return front ;}
Set {front = value ;}
}
Public int Back
{
Get {return back ;}
Set {back = value ;}
}
Public int MaxLength
{
Get {return size ;}
Set {size = value ;}
}
Public int Length ()
{
Return (back-front + size) % size;
}
Public bool IsEmpty ()
{
Return (front = back );
}
Public bool IsFull ()
{
Return (back + 1) % size = front );
}
Public void Clear ()
{
Front = back =-1;
}
Public void IN (T items)
{
If (IsFull ())
{
Throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is full ");
}
Item [++ back] = items;
}
Public T Out ()
{
T tmp = default (T );
If (IsEmpty ())
{
Throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is empty ");
}
Tmp = item [++ front];
Return tmp;
}
Public T GetFrontItem ()
{
If (IsEmpty ())
{
Throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is empty ");
}
Return item [front + 1];
}
}
Test queue code:
Code
Public class Program
{
Static void Main (string [] args)
{
Try
{
JQueen <string> JQ = new JQueen <string> ();
Console. WriteLine (JQ. IsEmpty (); // whether it is null
Console. WriteLine (JQ. IsFull (); // whether the team is full
& N