Node encountered when inserting data Java version of the data structure is empty question

Empty node encountered problems head interpolation code demo version of Java data structures and algorithms textbook. First the code.

Objectlist

Import java.util.Scanner;
 
 public  class ListLinked <T> { 
     ListLinkedNode <Integer> = head new new ListLinkedNode <Integer> (); // declaration head node
     // add node 
    public   void addFromHead ( int E) { 
        ListLinkedNode <Integer > p = new new ListLinkedNode <Integer> (); // declare and create the node p is the node insertion 
        p.setData (E); 
        p.setNext (head.getNext ()); 
        head.setNext (p); // after inserted into the head node p 
    }
     // Create a list head interpolation 
    public  static ListLinked <Integer>createFromHead () { 
        ListLinked <Integer> = listLinked new new ListLinked <> (); // initialize list 
        System.out.println ( "- establishing the list head interpolation -" ); 
        System.out.println ( "Please enter the chain length: " ); 
        Scanner SC = new new Scanner (the System.in);
         // n-: chain length 
        int n-= sc.nextInt (); 
        System.out.println ( " enter a value: " );
         for ( int I = 0; I <n-; I ++ ) { 
            of System.out.print ( "Please enter" + (i + 1) + " values:" );
             int E = SC.nextInt ();
            listLinked.addFromHead(e);
        }
        System.out.println("链表创建完毕");
        return listLinked;
    }
    //输出
    public void display() {
        System.out.println("{");
        ListLinkedNode<Integer> p=new ListLinkedNode<Integer>();
        p=head.getNext();
        while(p!=null) {
            int value=p.getData();
            System.out.println(value);
            p=p.getNext();
        }
        System.out.println("}");
    }

}

 

Node Class

public  class ListLinkedNode <T> {
     // data members 
    Private T Data;
     Private ListLinkedNode <T> Next;
     // get the data field 
    public T the getData () {
         return Data; 
    } 
    // set the data field 
    public  void the setData (T Data) {
         the this .data = Data; 
    } 
    // get pointer field 
    public ListLinkedNode <T> getNext () {
         return Next; 
    } 
    // set the pointer field 
    public  void SetNext (ListLinkedNode <T>Next) {
         the this .next = Next; 
    } 
    // have argument constructor 
    public ListLinkedNode (T Data) {
         the this .data = Data;
         the this .next = null ; 
    } 
    // no argument constructor 
    public ListLinkedNode () { 
        
    } 
}

 

Test category

public class test {
    public static void main(String[] args) {
        ListLinked list=new ListLinked();
        list.createFromHead();
        list.display();
    }

}

 

When tested, it is determined whether p is empty when the implementation of output, p has been null. 

Break point debugging, I noticed the problem.

 

 

Since the java reference type as a pointer, in the method in addFromHead, in the next head address stored object p.

When the addFromHead method, the end of the life cycle of the local variable p is taken away by garbage collection mechanism

No stack variables point to before p object on the heap, the heap objects are also used as the garbage was taken away, head of the next, it is a null

 

Guess you like

Origin www.cnblogs.com/zrx7/p/11495120.html