Basic operation of Linked list Java language implementation

Source: Internet
Author: User

 Packagecom.baorant; Public classJavademo { Public Static voidMain (string[] args) {linklist linklist=Newlinklist (); Linklist.addfirstnode (20); Linklist.addfirstnode (21st); Linklist.addfirstnode (19); //19,21,20Linklist.add (1, 22);//19,22,21,20Linklist.add (2, 23);//19,22,23,21,20Linklist.add (3, 99);//19,22,23,99,21,20linklist.displayallnodes (); //node node = Linklist.deletefirstnode (); //System.out.println ("node:" + Node.data); //linklist.displayallnodes (); //node = linklist.deletebypos (2); //System.out.println ("node:" + Node.data); //linklist.displayallnodes (); //Linklist.deletefirstnode ();Node node = linklist.deletebydata (19); //node node = linklist.deletebypos (0);System.out.println ("Node:" +node.data);        Linklist.displayallnodes (); Node Node1= Linklist.findbypos (0); System.out.println ("Node1:" +node1.data); Node Node2= Linklist.findbydata (22); System.out.println ("Node2:" +node2.data); }}/*** Single node * *@authorBaorant **/classNode {protectedNode Next;//pointer Field    protected intData//data fields     PublicNode (intdata) {         This. data =data; }    //Show this node     Public voiddisplay () {System.out.print (data+ " "); }}/*** Single Linked list * *@authorBaorant **/classlinklist { PublicNode first;//define a head node.    Private intpos = 0;//the location of the node     Publiclinklist () { This. First =NULL; }    //Insert a head node     Public voidAddfirstnode (intdata) {Node Node=NewNode (data); Node.next=First ; First=node; }    //deletes a head node and returns the head node .     Publicnode Deletefirstnode () {node Tempnode=First ; First=Tempnode.next; returnTempnode; }    //Insert a node at any point after index     Public voidAddintIndexintdata) {Node Node=NewNode (data); Node Current=First ; Node previous=First ;  while(POS! =index) {Previous=Current ; Current=Current.next; POS++; } Node.next=Current ; Previous.next=node; POS= 0; }    //remove nodes from any location     PublicNode Deletebypos (intindex) {Node current=First ; Node previous=First ;  while(POS! =index) {POS++; Previous=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; } Else{pos= 0; Previous.next=Current.next; }        returnCurrent ; }    //Delete nodes based on the data of the node (delete only the first one)     PublicNode Deletebydata (intdata) {Node current=First ; Node previous= First;//Remember the previous node         while(Current.data! =data) {            if(Current.next = =NULL) {                return NULL; } Previous=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; } Else{Previous.next=Current.next; }        returnCurrent ; }    //Show all the node information     Public voiddisplayallnodes () {Node current=First ;  while(Current! =NULL) {current.display (); Current=Current.next;    } System.out.println (); }    //finding node information based on location     PublicNode Findbypos (intindex) {Node current=First ; if(POS! =index) { Current=Current.next; POS++; }        returnCurrent ; }    //finding node information based on data     PublicNode FindByData (intdata) {Node current=First ;  while(Current.data! =data) {            if(Current.next = =NULL)                return NULL; Current=Current.next; }        returnCurrent ; }}

Basic operation of Linked list Java language implementation

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.