Java implementation single linked list Flip __java

Source: Internet
Author: User

This post is good, respect the original version of the original paste

(i) the node structure of a single linked list:
Data field: The domain that stores the data element information is called the domain;
next field: the domain where the immediate successor is stored is called the Pointer field, which is the Pointer field (chain field) that holds the direct successor address (location) of the node.
Data field + next field : A storage map that composes the AI, called a node ;
Note: The ① link lists the n nodes of the linear table in their logical order by the chain field of each node.
② Each node has only one chain field list called a single linked list.
The so-called linked list is like a train compartment, starting from the locomotive, each car is attached to the rear of the car.
To implement a single list store, you first create a node class whose Java code is as follows:

Class Node {  
    private int data;//data domain  
    private node next;//pointer domain public  
    node (int Data) {  
        //super ();  
        This. data = data;  
    }  
    public int GetData () {return  
        Data;  
    }  
    public void SetData (int Data) {this  
        . data = data;  
    }  
  
    Public Node GetNext () {return  
        Next;  
    }  
    public void Setnext (Node Next) {this  
        . Next = next;  
    }  

(ii) Ways to achieve reversal:
(1) Recursive inversion method
: Reverses the subsequent nodes before reversing the current node. This begins with the beginning of the start, and the depth of the layer until the tail node begins to reverse the pointer field. The simple point is to start from the tail node, reverse inversion of each node pointer field points, the process diagram is as follows:
Head :Is the pointer field of the previous node (PS: The pointer field of the previous node points to the current node)
Head.getnext ():Is the pointer field of the current node (PS: The pointer field of the current node points to the next node)
Rehead:Is the head node of the new linked list after inversion (i.e. the tail node of the original single linked list)


The Java code is as follows:

    Package javatest1;  
            public class Javatest1 {public static void main (string[] args) {node head = new Node (0);  
            Node Node1 = new node (1);  
            Node Node2 = new node (2);  
            Node Node3 = new node (3);  
            Head.setnext (Node1);  
            Node1.setnext (Node2);  
      
            Node2.setnext (NODE3);  
            The linked table Node h = head before printing inversion;  
                while (null!= h) {System.out.print (H.getdata () + "");  
            h = H.getnext ();  
      
            //Call inversion method head = Reverse1 (head);  
            System.out.println ("\n**************************");  
                The result of the print reversal while (null!= head) {System.out.print (Head.getdata () + "");  
            Head = Head.getnext (); /** * Recursion, reverses the following nodes before reversing the current node/public static node Reverse1(Node head) {//Head is considered to be the previous node, Head.getnext () is the current node, Rehead is the first node of the new linked list after inversion if (header = null | | head.getnext () = n ull) {return head;//if the empty chain or the current node in the tail node, then directly back to the node Rehead = Reverse1 (head.get Next ())//Reverses the successor node Head.getnext () Head.getnext (). Setnext (head);//The Pointer field of the current node is referred to as the forward node Head.setnext (  
            NULL);//The Pointer field order for the previous node is null;  
            Return rehead;//the first node of the new linked List} class Node {private int data;//data field  
                Private node next;//pointer domain public node (int Data) {//Super (); This.  
            data = data;  
            public int GetData () {return Data; public void SetData (int Data) {this.  
            data = data;  
            Public Node GetNext () {return Next;  
      
    }        public void Setnext (Node Next) {this.  
            Next = next;   }  
        }

(2) Traverse reversal method:The recursive inversion method reverses the direction of the pointer field from the backward reverse order, and the traversal inversion is the direction of the pointer field that reverses each node from the point of departure. The basic idea is to cache the next node of the current node cur cur.getnext () after the temp, and then change the current node pointer to the previous knot pre. That is, before reversing the current node pointer point, the pointer field of the current node is temporarily saved with TMP to be used for the next time, and the procedure can be expressed as follows:
Pre:Previous node
cur:Current node
tmp:A temporary node that holds the pointer field (that is, the next node) for the current node.


Java Code Implementation:
    Package javatest1;  
            public class JavaTest1 {public static void main (string[] args) {node head = new Node (0);  
            Node Node1 = new node (1);  
            Node Node2 = new node (2);  
      
            Node Node3 = new node (3);  
            Head.setnext (Node1);  
            Node1.setnext (Node2);  
      
            Node2.setnext (NODE3);  
            The linked table Node h = head before printing inversion;  
                while (null!= h) {System.out.print (H.getdata () + "");  
            h = H.getnext ();  
            //Call reverse method/head = REVERSE1 (head);  
      
            Head = Reverse2 (head);  
            System.out.println ("\n**************************");  
                The result of the print reversal while (null!= head) {System.out.print (Head.getdata () + "");  
            Head = Head.getnext (); }/** * Traversal, cache the next node of the current node and change the current nodepointer/public static node Reverse2 (Node head) {if (head = = null) retur  
            n Head;   
            Node pre = head;//Node cur = head.getnext ()//Current node node tmp;//temporary node to hold the pointer field of the current node (that is, next node)  
                while (cur!= null) {//current node is NULL, description is located at tail node TMP = Cur.getnext ();  
                Cur.setnext (PRE)//Reverse pointer field pointing//pointer moving down pre = cur;  
            cur = tmp;  
              
            //Finally, the pointer field of the head node of the original linked list is null, and the head nodes of the new linked list are returned, that is, the tail node head.setnext (null) of the original list;  
        return pre;  
      
        class Node {private int data;//data field private node next;//pointer field  
            Public Node (int Data) {//Super (); This.  
        data = data;  
        public int GetData () {return Data; } public void SetData (int Data) {this.  
        data = data;  
        Public Node GetNext () {return Next; public void Setnext (Node Next) {this.  
        Next = next;   }  
    }



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.