2016-04-03 16 views
0

ne olacak? main()'un bu kodu açıklama ile birlikte yürütmesini istiyorum. Bağlantılı listeyi bir dizi gibi doldurmaya ve bu listedeki öğeleri yazdırmaya çalışıyorum.Listeyi kullanıcı tanımlı değerlerle doldurmak ve sonunda listeyi yazdırmak istiyorum. Ana()

Veri yapılarında yeniyim ve konsepti çok net ve Java olarak almak istiyorum. Herhangi bir yardım çok takdir edilecektir.

Kodum:

public class LinkedList { 
    private LinkedList next; //just creating getters and setters for the next node 
    private int data; //just creating getters and setters for the data 
    public LinkedList(int data) { 
     this.data=data; //default constructor to input a value to the node 
    } 
    public LinkedList getNext() { 
     return next; //getter method for the object (probably returns the value of the address of the current object) 
    } 
    public void setNext(LinkedList next) { 
     this.next = next; //setter method for the object (probably to set the address of the next node) 
    } 
    public int getData() { 
     return data; //getter method for the data (probably to get the data of the current node) 
    } 
    public void setData(int data) { 
    this.data = data;//setter method for the data (probably to set the data in the current node) 
    } 
} 

cevap

0
şimdi geliştirmek gerekir :)

public static void main(String[] args) { 

    // populate from array 
    int[] data = {1,2,3}; 
    LinkedList myList = new LinkedList(0); 
    LinkedList l = myList; 
    for (int i = 0 ; i < data.length ; i++) { 
     l.setData(data[i]); 
     if (i == data.length - 1) continue; 
     l.setNext(new LinkedList(0)); 
     l = l.getNext(); 
    } 

    // print 
    l = myList; 
    while(l != null) { 
     System.out.print(l.getData() + ","); 
     l = l.getNext(); 
    } 
} 

kukla örnek