Simple data structure (ii) queue

Source: Internet
Author: User

Queue

Sequential queue: A contiguous amount of storage space is used to store data elements in the queue, which are called sequential queues (Sequence queue). Use a one-dimensional array to hold the data elements in the sequential queue. The team head position is positioned at the end of the array labeled 0, denoted by front, and the tail position is located at the other end of the array, denoted by rear. Front and rear vary with insertions and deletions. When the queue is empty, front=rear=-1.
The graph is a diagram of the two indicators of the sequential queue and the data elements in the queue.

Queue: The elements in the queues start at the end of the team and slowly line up.

Out of line: The elements of the queue start out from the first team

Sequential queue-(circular sequential queue): Overflow occurs if another data element is queued. But in fact the queue is not full, there is free space, this phenomenon is called "false overflow." This is due to the operation Principle of queue "team tail in the first out". The method of solving false overflow is to treat the sequential queue as the loop structure of the end-to-end, the relationship of the tail-end indicator is constant, and this kind of queue is called the cyclic sequential queue (Circular sequence). Loop queue.


Queues from the system:

Queues in the BCL

c#2.0 the non-generic queue class is available in the following versions

C#2.0 provides a generic queue<t> class
Method
1,enqueue () Join the team (in the first place)
2,dequeue () Squad (remove the first element of the team and return the removed element)
3,peek () Gets the first element of the team, does not remove
4,clear () empty element
5,count gets the number of elements in the queue

Program implementation:

          //1. Using Queues in the BCLqueue<int> queue =Newqueue<int>(); //QueueQueue. Enqueue ( at);//Team FirstQueue. Enqueue ( $); Queue. Enqueue ( the); Queue. Enqueue ( the); Console.Write ("23 45 67 89 were added after the queue is:"); foreach(intAinchqueue) {Console.Write (a+" ");            } Console.WriteLine (); Console.WriteLine ("after 23 45 67 89 has been added, the size of the queue is:"+queue.            Count); //out of the team (get the first team data and delete)            inti =queue.            Dequeue (); Console.WriteLine ("the data for the first team is:"+i); Console.WriteLine ("the size of the queue after the team is:"+queue.            Count); intj =queue.            Peek (); Console.WriteLine ("the results from Peek are:"+j); Console.WriteLine ("the size of the queue after peek is:"+queue.            Count); Queue.            Clear (); Console.WriteLine ("the size of the queue after clear is:"+queue.            Count); Console.readkey ();


Results:

Implement the queue yourself:

Queue Interface Definition

Internal InterfaceIqueue<t>    {        intCount {Get; }//Gets the queue Length property        intGetLength ();//to find the length of a queue        BOOLIsEmpty ();//to determine if the column is empty        voidClear ();//Empty Queue        voidEnqueue (T item);//QueueT Dequeue ();//out TeamT Peek ();//take the team head element}

Implementation of sequential queues:

classSeqqueue<t>:iqueue<t>    {        Privatet[] data; Private intCount//represents the current number of elements         Private intFront//Team Head (team first element index minus one)        Private intRear//team Tail (tail element Index)//constructor Function         PublicSeqqueue (intsize) {Data=NewT[size]; Count=0; Front= -1; Rear= -1; }        //default constructor         PublicSeqqueue (): This(Ten)        {        }        //returns the number in the queue         Public intCount {Get{returncount;} }        //Get Queue Length         Public intGetLength () {returncount; }        //determine if the queue is empty         Public BOOLIsEmpty () {returnCount = =0; }        //Clear Queue         Public voidClear () {count=0; Front= Rear =-1; }        //Add to Queue         Public voidEnqueue (T item) {if(count==data. Length) {Console.WriteLine ("The queue is full and no new data can be added"); }            Else            {                if(Rear = = data. length-1) {data[0] =item; Rear=0; }                Else{data[rear+1] =item; Rear++; } Count++; }        }        //Remove the first element of the team and return the value         PublicT Dequeue () {if(Count >0) {T temp= Data[front +1]; Front++; Count--; returntemp; }            Else{Console.WriteLine ("queue is empty, unable to get data for first team"); return default(T); }        }        //get team first element not removed         PublicT Peek () {T temp= Data[front +1]; returntemp; }    }

The results are the same as before and are not posted here.

Stack queue: Another way to store a queue is chained storage, a queue called a chain queue (Linked). The chain queue is usually represented by a single-linked list, and its implementation is a simplification of the single-linked list. So, the node structure of the chain queue is the same as a single linked list. Because the chain queue operation is only at one end, for ease of operation, the team head is set to the head of the list, and does not require a head node.

Chain Queue Node Class:

classNode<t>    {        PrivateT data;//data fields        PrivateNode<t> Next;//reference Domain//Construction Method         PublicNode (T data) { This. data =data; }        //data Field Properties         PublicT Data {Get{returndata;} Set{data =value;} }        //referencing Domain Properties         PublicNode<t>Next {Get{returnNext;} Set{next =value;} }    }

Implementation of the stack queue:

classLinkqueue<t>:iqueue<t>    {        PrivateNode<t> Front;//Head knot Point        PrivateNode<t> Rear;//Tail Node        Private intCount//indicates the number of elements         PublicLinkqueue () {Front=NULL; Rear=NULL; Count=0; }         Public intCount {Get{returncount;} }         Public intGetLength () {returncount; }         Public BOOLIsEmpty () {returnCount = =0; }         Public voidClear () {Front=NULL; Rear=NULL; Count=0; }         Public voidEnqueue (T item) {Node<T> newnode=NewNode<t>(item); if(Count = =0) {Front=NewNode; Rear=NewNode; Count=1; }            Else{rear. Next=NewNode; Rear=NewNode; Count++; }        }         PublicT Dequeue () {if(Count = =0) {Console.WriteLine ("the column is empty and cannot be out of the queue"); return default(T); }            Else if(Count = =1) {T Item=Front.                Data; Front= Rear =NULL; Count=0; returnitem; }            Else{T Item=Front.                Data; Front=Front.                Next; Count--; returnitem; }        }         PublicT Peek () {if(Front! =NULL)            {                returnFront.            Data; }            Else            {                return default(T); }        }    }

The result is ibid.

Simple data structure (ii) queue

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.