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 = 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