Java implementation Queue--intra-queue uses chained storage structure

Source: Internet
Author: User

Java implementation Queue--intra-queue uses chained storage structure


Chain queue


Code:

package hash;/** * created with intellij idea. * user: asus  * date: 14-9-17 * time:  Morning 11:58 * to change this template  use File | Settings | File Templates. */public class  customlinkqueue<e> {    //defines an internal class Node,node instance that represents the node of the chain stack.     private class node {        // Save node's data         private E data;         //reference to next node         private Node next;         //constructor with no parameters          Public node ()  {        }         //initializes the data domain of the node      &nbsP;  private node (E data)  {             this.data = data;        }         //constructor for initializing all properties         public  Node (E data, node next)  {             this.data = data;             this.next = next;        }    }     private node front;  //head pointer points to head node     private  node rear;   //Tail Node     private int count; // Number of the queue elements     /**     *  initialize queue      *   This queue is empty      */&Nbsp;   public customlinkqueue ()  {         Node p = new node ();        p.data =  null;        p.next = null;         front = rear = p;    }    /**      *  inserting nodes at the back end of a queue      *     *   @param  item     */    public void enqueue (E  item)  {        node newnode = new node ( );        newnode.data = item;         newnode.next = null; //Queued Node No successor          this.rear.next&nbsp= newnode; //the successor node of the original tail node points to the new node         this.rear =  newnode;     //rear point to last node          count++;    }    /**     *  out Team       *  Delete nodes at the front of the queue      *     * @ Return     */    public e dequeue ()  throws  exception {        if  (IsEmpty ())  {             throw new exception ("Queue is empty");         } else {             e obj;            node p  = this.front.next;   //point to the first node of the team head             obj =  p.data;            this.front.next =  p.next;            if  (REAR&NBSP;==&NBSP;P)  {                rear  = front;            }             count--;             return obj;        }     }    /**     *  @return      */     public int size ()  {         Return count;    }    /**     *  Traversal Algorithm       *  move the front pointer until the front pointer catches the rear pointer      */    public  Void traverse ()  {        for  (node current =  front.next; current != null; current = current.next)  {             system.out.println (Current.data);         }    }    /**      *  the condition that the queue is empty is front == rear     *      *  @return      */    public boolean isempty ()  {        return front == rear;     }    Public static void main (string args[])  throws Exception {         customlinkqueue linkqueue = new customlinkqueue ();         for  (int i = 0; i < 5; i+ +)  {  //Add 5 elements              Linkqueue.enqueue ("LyX"  + i);        }         system.out.println (Linkqueue.size ());         system.out.println ("===========traverse===========");         Linkqueue.traverse ();         system.out.println ("===================== ========= ");         linkqueue.dequeue ();         linkqueue.deqUeue ();         system.out.println ("===========traverse===========");         linkqueue.traverse ();         system.out.println ("==============================");         system.out.println (Linkqueue.size ());     }}


====en====


Java implementation Queue--intra-queue uses chained storage structure

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.