[List] some common sense on the list - Summary

① list recently engaged in a little Mongolian, understand the truth, but in the hands of code knocking on the list when encountered problems. I finally found to this article, I suddenly know better the list (reached the point to write code).

    https://m.jb51.net/article/170155.htm?tdsourcetag=s_pcqq_aiomsg

② First, java, each node linked list represented by Node class, the nodes are defined as follows:

. 1  Package com.linkedlist;
 2  
. 3  public  class the Node {
 . 4    Private  int Data; // node data 
. 5    Private the Node Next; // next node 
. 6  
. 7    public the Node ( int Data) {
 . 8      the this .data = Data;
 . 9    }
 10  
. 11    public  int the getData () {
 12 is      return Data;
 13 is    }
 14  
15    public  void the setData ( int data) {
16     this.data = data;
17   } 
18 
19   public Node getNext() {
20     return next;
21   }
22 
23   public void setNext(Node next) {
24     this.next = next;
25   }
26 }

③ Then, the operation class defines a list, which contains common operations:

    

Package com.linkedlist; 

Import java.util.Hashtable; 

public  class LinkedListOperator {
   Private the Node head = null ; // head node. java, allowing written: Let a head start node is empty 

  // add a node at the end of the linked list 
  Private  void the addNode ( int Data) {     // argument passed Data 
    the Node = the newNode new new the Node (Data);   / / introduced to the data, the data was performed to get the line ② in use 4,8,16 
    IF (head == null ) { 
      head = the newNode;
       return ; 
    } 
    the Node TEMP = head;// temp is copied body head, after which the matter temp variations are irrelevant how things head 
    the while (temp.getNext ()! = Null ) {    // infinite loop, temp until they have a tail node 
      temp = temp.getNext ( );   // the next node of temp, temp assigned their 
    } 
    temp.setNext (the newNode);    // after the circulation has been to ensure that the tail node temp is a list of the original, 
  }                                        // connected sentences, so temp newNode become the next node
                     // the above-described function is addNode

④ To be continued, I sleep for a nap

Guess you like

Origin www.cnblogs.com/zf007/p/11571823.html
Recommended