Big talk Data Structure IX: chain-type storage structure of queues (chain queues)

Source: Internet
Author: User
Tags empty int size

1. The characteristics of the chain queue:

Chain queue is actually a single linked list, but it is the first out of a single linked list, in order to achieve convenience, the program set up the team head (front), Team tail (rear) two pointers.

2. Java uses a linked list implementation queue:

Node class, data that contains nodes, and references to the next node public  
class Node<e> {  
    private E data;//field  
    private node<e> next; The Pin field holds the reference to the next node public node  
      
    () {} public node  
      
    (E data) {  
        this.data = data;  
    }  
      
    Public Node (E data, node<e> next) {  
        this.data = data;  
        This.next = next;  
      
    Public E GetData () {return  
        data;  
    }  
      
    public void SetData (E data) {  
        this.data = data;  
    }  
      
    Public node<e> GetNext () {return  
        next;  
    }  
      
    public void Setnext (node<e> next) {  
        this.next = next;  
    }  
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Java implementation chain queue public class Linkedqueue<t> {private node<t> front;/Remember team head knot private NODE&LT;T&G T Rear; Remember team tail node private int size;  
        Remember that the list length is public linkedqueue () {front = rear = NULL;  
    size = 0; //Append element to queue tail public boolean enqueue (T data) {node<t> NewNode = new Node<t> (da  
        TA);  
        if (IsEmpty ()) {//To determine whether the queue is empty front = rear = NewNode;  
        else {rear.setnext (newNode);//////Set the new node to the end node rear = NewNode;  
        } size++;  
        SYSTEM.OUT.PRINTLN (Data + "team ...");  
    return true;  
        The first element of the queue header is out of the team public T dequeue () {T data = null;  
        if (IsEmpty ()) {System.out.println ("queue is empty, cannot be out of team ...");  
            else {if (front.getnext () = null) {//Queue only one node rear = null; data = Front.getdata (); 
            Front = Front.getnext ();  
            Set the next node of the original team head to the team head System.out.println (data + "out team ...");  
        size--;  
    } return data;  
    //Get the length of the chain queue public int size () {return size;  
    //Determine if the chain queue is null public boolean IsEmpty () {return size = = 0;  
        }//Empty chain queue public void clear () {front = rear = NULL;  
    size = 0; The element public void display () {if (!isempty ()) {node<t> Nextn in the print queue  
            Ode = Front;  
                for (int i = 0; i < size (); i++) {System.out.print ("" + Nextnode.getdata ());  
            NextNode = Nextnode.getnext (); "}}//test method public static void main (string[] args) {Linkedqueue<str  
        ing> queue = new linkedqueue<string> ();  
        Queue.enqueue ("John"); Queue.dequeue ();  
        Queue.dequeue ();  
        Queue.enqueue ("Dick");  
        Queue.enqueue ("Harry");  
        Queue.enqueue ("Zhao Liu");  
        Queue.dequeue ();  
        Queue.enqueue ("Tianqi");  
        Queue.dequeue ();  
        Queue.enqueue ("Zhou Ba");  
        System.out.println ("The number of elements in the queue is:" + queue.size ());  
        System.out.print ("Elements in the print queue:");  
    Queue.display (); }  
}

3. Chain Queue and loop queue comparison:

1.) Time: Circular queue in advance to apply for a good space, the use of the period is not released. The chain queue has some time overhead for each application and release node, and if the team is operating frequently, the chain queue performance is slightly worse.

2.) Space: The loop queue must have a fixed length, so there is a problem with the number of storage elements and the waste of space. Chain queues do not have this problem, so they are more flexible in space.

4. When to use chain queues:

1. In cases where the maximum queue length can be determined, a circular queue is recommended.

2.) If the length of the queue cannot be estimated, the chain queue is used.

Author: csdn Blog zdp072

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.