Exercises of two-terminal queue algorithm

Source: Internet
Author: User
Tags data structures int size

In the words of the university teacher said sister is more important than work ~, work can be changed, sister this ... So... These two months have been busy fall in love, hi, slowly adjust the state of mind, this article on the selection of a simple data structure to chat, saying that there are many data structures are playing combination boxing, such as: Block linked list, block array, of course, there are two-terminal queue, yes, it is the stack and queue of the combination.

One: Concept

We know that the normal queue is a restricted level, the other end of the FIFO form, the stack is one end in and out of the LIFO form, and the two-terminal queue does not have such a restrictive level, that is, we can at both ends of the queue to insert or delete operations.

Two: Coding

1: Define the structure body

Usually, the interior of the queue is implemented using an array, and with two pointer head and tail to point to the interval of the array, in order to make full use of the array space, we will also use% to implement the logical loop array, as shown below.

public class Myqueue   
     {public   
         int head;   
        
         public int tail;   
        
         public int maxSize;   
        
         public int size;   
        
         Public t[] list;   
        
         Public Myqueue ()   
         {head   
             = tail = size = 0;   
             MaxSize = 3;   
             List = new T[maxsize];   
         }   
     

One of the notable details here is the Size field, which is to make it easier to count whether the queue is full or if the queue is empty.

2: Team tail Teams

I just said that. The two-terminal queue can be inserted and deleted at both ends of the queue, and note that when we use head and tail pointers, the tail pointer is the next position to the element, and the head pointer points to the current element, so we can push the data from the tail end as long as " Move down one position clockwise.

 

<summary>   
     ///team   
     ///</summary>   
     ///<param name= "T" ></param>   
     /// <returns></returns> Public   
     bool Push_tail (T t)   
     {   
         //judge whether the queue is full   
         if (myqueue.size = = MyQueue.list.Length) return   
             false;   
        
         Myqueue.list[myqueue.tail] = t;   
        
         Clockwise rotation   
         Myqueue.tail = (myqueue.tail + 1)% Myqueue.maxsize;   
        
         myqueue.size++;   
        
         return true;   
     }

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.