2014-10-10 25 views
5

Ho creato correttamente una LinkList da zero. Finora può solo aggiungere dati. Nessuna cancellazione o niente di simile.Come stampare i dati nella mia LinkedList

Posso aggiungere stringhe, numeri interi ecc. Ma ho un problema con la stampa dei dati che ho aggiunto. Come lo faccio? Immagino che dovrò farlo prima, ma come?

Qui è la mia classe Node:

public class Node { 
    T data; 
    Node<T> nextNode; 

    public Node(T data) { 
     this.data = data; 
    } 

    public String toString() { 
     return data +""; 
    } 
} 

Ecco la classe LinkedList:

public class LinkedList <T> { 

Node<T> head; 
Node<T> tail; 

public void add (T data) { 
    // where to add statements. if its empty or not 
    Node<T> node = new Node<T> (data); 

    if (tail == null) { // empty list 
     // nothng in the node = tail = node; 
     head = node; 
     tail = node; 
    } 
    else { // non empty list, add the new boogie train to the tail 
     tail.nextNode = node; // new node pointing to tail 
     tail = node; // update 
    } 
} 

E qui è il principale. Dove creo un oggetto fuori da Linkedlist e utilizzo il metodo di aggiunta generico per aggiungere i miei dati. Ma come lo stampo sullo schermo? Grazie in anticipo.

public static void main(String[] args) { 
    LinkedList<Object> list = new LinkedList<Object>(); 
    list.add(15); // boogie1 = head 
    list.add(16); 
    list.add(10); // boogie end = tail 
+0

Ci scusiamo per il codice incasinato. Sembra che si rovini sempre quando provo ad aggiungere del codice. – Kharbora

+0

Devi solo copiarlo e premere Ctrl + K. Se lo avessi formattato correttamente e usato spazi per il rientro, rimarrà in questo modo. – EpicPandaForce

risposta

1

Devi eseguire l'override del metodo toString() nella classe LinkedList<T>

6

Aggiungere un metodo toString alla classe LinkedList

public String toString() { 
    Node<T> curr = head; 
    StringBuilder sb = new StringBuilder(); 
    sb.append("LinkedList ["); 
    while (curr != null) { 
     sb.append(curr.data); 
     if (curr.nextNode != null) { 
      sb.append(", "); 
     } 
     curr = curr.nextNode; 
    } 
    sb.append("]"); 
    return sb.toString(); 
} 

Poi lo chiamano nel metodo principale:

System.out.println(list.toString()); 
+0

Complicato e difficile da capire il tuo codice, ma grazie. – Kharbora

+0

cosa trovi difficile a riguardo? – yunandtidus

1

Bene, è possibile implementare il pattern Iterator: http://sourcemaking.com/design_patterns/iterator/java/1

O semplicemente implementare un metodo che può o stampare l'elemento di nodo, o eseguire qualcosa su ciascuno di loro, un po 'come questo:

public class LinkedList <T> { 

    Node<T> head; 
    Node<T> tail; 

    public void add (T data) { 
     ... 
    } 

    public void forEach(java.util.function.Consumer<T> consumer) 
    { 
     for(Node<T> currentNode = head; currentNode != null; currentNode = currentNode.nextNode) 
     //I am assuming the last node points to null in nextNode 
     // and that head is initialized to null if the list is empty 
     { 
      consumer.accept(currentNode); 
     } 
    } 
} 

Poi basta fare

linkedList.forEach(x -> System.out.println(x.toString()); 

Se tutto è giusto, questo dovrebbe funzionare in Java 8.

1

creare un metodo getter nella classe LinkedList.

public Node getHead() { 

    return head; 
} 

nel vostro main()

public static void main(String[] args) { 

    LinkedList<Object> list = new LinkedList<>(); 
    list.add(15); // boogie1 = head 
    list.add(16); 
    list.add(10); // boogie end = tail 

    Node node = list.getHead(); 

    // Break the loop after the variable reaches null, i.e. end of list. 
    // The un initialised instance non-primitive variable is always null by default. 
    while(node != null) { 

     System.out.println(node); // Calls the toString() from class Node. 
     node = node.nextNode; // Move to next node. 
    } 
} 

Spero che questo funziona per voi.

Problemi correlati