Java to implement its own bidirectional linked list linklist

Source: Internet
Author: User

/*** <p> * Node doubly linked list entity class * <p> * *@author<a href= "Mailto:[email protected]" >yangkj</a> *@version * @sinceAugust 15, 2016*/ Public classNode {//doubly linked List-previous nodeNode Previous; //doubly linked List-current node objectObject obj; //doubly linked list-next nodeNode Next;  PublicNode () {Super(); }     PublicNode (node Previous, Object obj, node next) {Super();  This. Previous =previous;  This. obj =obj;  This. Next =Next; }     PublicNode getprevious () {returnprevious; }     Public voidsetprevious (Node previous) { This. Previous =previous; }     PublicObject getobj () {returnobj; }     Public voidsetobj (Object obj) { This. obj =obj; }     PublicNode GetNext () {returnNext; }     Public voidSetnext (Node next) { This. Next =Next; }}
/*** * <p> * mylinklist bi-directional list * <p> * *@author<a href= "Mailto:[email protected]" >yangkj</a> *@version * @sinceAugust 15, 2016*/ Public classMylinklist {/*** First Node*/    PrivateNode Firstnode; /*** Tail node*/    PrivateNode Lastnode; /*** Number of nodes*/    Private intsize;  Publicmylinklist () {Super(); }    /*** Add Node * *@paramobj*/     Public voidAdd (Object obj) {if(Firstnode = =NULL) {            //If the linked list is empty, the current object is set to the first node and also the tail node, and the previous node and the next node are nullNode node =NewNode (NULLObjNULL); Firstnode=node; Lastnode=node; } Else {            //add a new node directly behind the LastnodeNode node =NewNode (lastnode, obj,NULL); //the next node of the old tail node points to the new plus nodelastnode.setnext (node); //the tail node of the linked list is set to the new nodeLastnode =node; } size++; }    /*** Insert node at specified index location * *@paramIndex *@paramobj*/     Public voidAddintindex, Object obj) {Node temp=node (index); if(Temp! =NULL) {Node up=temp.previous; Node NewNode=NewNode (up, obj, temp);            Up.setnext (NewNode);            Temp.setprevious (NewNode); Size++; }    }    /*** Get the specified Node object * *@paramIndex *@return     */     PublicObject Get (intindex) {Node temp=node (index); if(Temp! =NULL) {            returnTemp.obj; } Else {            return NULL; }    }    /*** Cross-border detection * *@paramIndex*/    Private voidRangcheck (intindex) {        if(Index < 0 | | index >size) {            Throw Newarrayindexoutofboundsexception (index); }    }    /*** Remove the node of the specified index * *@paramIndex*/     Public voidRemoveintindex) {        //cross-border detectionNode temp =node (index); if(Temp! =NULL) {Node up=temp.previous; Node Down=Temp.next; Up.next=Down ; Up.previous=Up ; Temp=NULL; Size--; }    }    /*** Gets the node under the specified index * *@paramIndex *@return     */    PrivateNode Node (intindex)        {Rangcheck (index); Node Temp=NULL; if(Firstnode! =NULL) {Temp=Firstnode;  for(inti = 0; I < index; i++) {Temp=Temp.next; }        }        returntemp; }    /*** Set the value of the specified index *@paramIndex *@paramobj*/     Public voidSetintIndex,object obj)        {Rangcheck (index); Node Temp=node (index); if(temp!=NULL) {temp.setobj (obj); }    }    /*** Get the list length * *@return     */     Public intsize () {returnsize; }     Public Static voidMain (string[] args) {mylinklist linklist=Newmylinklist (); Linklist.add ("AAA"); Linklist.add ("BBB"); Linklist.add ("CCC"); System.out.println (Linklist.get (1)); Linklist.set (1, "FFF"); System.out.println (Linklist.get (1)); }}

Java to implement its own bidirectional linked list linklist

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.