Implementing a Java Queue queue based on a doubly linked list

Source: Internet
Author: User
Tags prev

In addition to using a one-dimensional array, a single-linked list implementation of the queue queues, but also through the double-linked list to implement queue queues.

When implementing a doubly linked list based on the Nlnode class, in order to make programming more concise, we usually have to set a dummy node (Dummy nodes) at the front and the end. These two nodes are called Head nodes (header node) and tail node (Trailer node) ㈠, which acts as Sentinel (Sentinel). That is, they do not store any real data objects, and the next (prev) Reference of the head (tail) node points to the first (last) node, while the Prev (next) reference is empty. The structure of the doubly linked list, as shown in the following code:

Java code:

Doublenode class:

 PackageCom.doub.list;/** * * @author gannyee * * * * Public  class doublenode {    //declared Element    PrivateObject element;//declared Prior    PrivateDoublenode Prior;//declared Next    PrivateDoublenode Next;//constructor     Public Doublenode(){ This(NULL,NULL,NULL); } Public Doublenode(Doublenode prior,object element, Doublenode next) { This. prior = prior; This. element = element; This. next = Next; }//get element     PublicObjectgetelement() {returnElement }//set element     Public void setElement(Object Element) { This. element = element; }//get Prior     PublicDoublenodeGetprior() {returnPrior }//set Prior     Public void Setprior(Doublenode Prior) { This. prior = prior; }//get Next     PublicDoublenodeGetNext() {returnNext }//set Next     Public void Setnext(Doublenode Next) { This. next = Next; }}

Doublelistqueue class:

 PackageCom.doub.list;ImportJava.util.Arrays;ImportCom.queue.ExceptionQueueEmpty; Public  class doublelistqueue {    //Declared head    Private StaticDoublenode Head;//declared rear    Private StaticDoublenode Rear;//declared size;    Private Static intSize//declared Length    Private Static intLength//Constructor     Public Doublelistqueue() {//initializeHead =NewDoublenode (); Rear =NewDoublenode ();        Head.setnext (rear); Head.setprior (NULL); Rear.setnext (NULL); Rear.setprior (head); This. Size = This. length =0; }//Get size     Public int GetSize() {returnSize }//get length     Public int length(){returnLength }//Is empty     Public Boolean IsEmpty() {returnSize = =0; }//Insert element in first position     Public void Insertfirst(Object Element) {Doublenode NewNode =NewDoublenode (Head,element,head.getnext ());        Head.getnext (). Setprior (NewNode);        Head.setnext (NewNode);        Size + +;    length = size; }//Insert element in last position     Public void Insertlast(Object Element) {Doublenode NewNode =NewDoublenode (Rear.getprior (), element,rear);        Rear.getprior (). Setnext (NewNode);        Rear.setprior (NewNode);        Size + +;    length = size; }//Remove element from first position     Public void Removefirst()throwsexceptionqueueempty{if(IsEmpty ())Throw NewExceptionqueueempty ("Queue is empty");        Head.getnext (). GetNext (). Setprior (head);        Head.setnext (Head.getnext (). GetNext ());    Size--; }//Remove element from the last position     Public void Removelast()throwsexceptionqueueempty{if(IsEmpty ())Throw NewExceptionqueueempty ("Queue is empty");        Rear.getprior (). Getprior (). Setnext (rear);        Rear.setprior (Rear.getprior (). Getprior ());    Size--; }//Get first node but not isn ' t deletion     PublicObjectGetFirst()throwsexceptionqueueempty{if(IsEmpty ())Throw NewExceptionqueueempty ("Queue is empty");returnHead.getnext (). GetElement (); }//Get last node and not isn ' t deletion     PublicObjectGetLast()throwsexceptionqueueempty{if(IsEmpty ())Throw NewExceptionqueueempty ("Queue is empty");returnRear.getprior (). GetElement (); }//get all Element     Public void getallelements() {object[] array =NewObject[getsize ()]; Doublenode Travelnode =NewDoublenode (); Travelnode = Head.getnext (); for(inti =0;            Travelnode! = Rear;i +) {Array[i] = travelnode.getelement ();        Travelnode = Travelnode.getnext (); } System.out.println ("All elements is:"+ arrays.tostring (array)); }}

Doublelistqueuetest class:

Packagecom. Doub. List;Importcom. Queue. Exceptionqueueempty;/** * * @author gannyee * * * *public class Doublelistqueuetest {public static void main (string[] args) throws Exceptionqueueempty {Doublelis Tqueue DLQ = new Doublelistqueue ();System. out. println("Size:"+ DLQ. GetSize());System. out. println("is empty?"+ DLQ. IsEmpty());for (int i =0; i < 6;i + +) {Dlq. Insertfirst(i);} System. out. println("Size:"+ DLQ. GetSize());System. out. println("is empty?"+ DLQ. IsEmpty());Dlq. Getallelements();System. out. println(DLQ. GetFirst());System. out. println(DLQ. GetLast());for (int i =0; i < 6;i + +) {Dlq. Insertlast(i +1);} System. out. println("Size:"+ DLQ. GetSize());System. out. println("is empty?"+ DLQ. IsEmpty());Dlq. Getallelements();System. out. println(DLQ. GetFirst());System. out. println(DLQ. GetLast());for (int i =0; i < 6;i + +) {Dlq. Removefirst();} System. out. println("Size:"+ DLQ. GetSize());System. out. println("is empty?"+ DLQ. IsEmpty());Dlq. Getallelements();System. out. println(DLQ. GetFirst());System. out. println(DLQ. GetLast());for (int i =0; i < 6;i + +) {Dlq. Removelast();} System. out. println("Size:"+ DLQ. GetSize());System. out. println("is empty?"+ DLQ. IsEmpty());Dlq. Getallelements();}}

Test results:

Size:0 is Empty?trueSize:6 is Empty?falseAll elements is: [5,4,3,2,1,0]50Size: A is Empty?falseAll elements is: [5,4,3,2,1,0,1,2,3,4,5,6]56Size:6 is Empty?falseAll elements is: [1,2,3,4,5,6]16Size:0 is Empty?trueAll elements is: []

References: Data structures and Algorithms (Java description) Deng Junhui

Implementing a Java Queue queue based on a doubly linked list

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.