C # data structure and algorithm series (5) queue

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.