Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print out the data in my LinkedList

Tags:

java

I have successflully created a LinkedList from scratch. So far it can only add data. No deletion or anything fancy like that.

I can add strings, integers etc but I have a problem with printing the data I have added. How do I do that? I guess I'll have to loop through it first, but how?'

Here is my Node class:

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

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

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

Here is the LinkedList class:

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  
    }   
}

And here is the main. Where I create an object out of Linkedlist and use the generic add method to add my data. But how do i print it out on the screen? Thanks in advance.

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
like image 209
Kharbora Avatar asked Oct 10 '14 09:10

Kharbora


People also ask

How can we print data from linked list?

PrintElement.java Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don't print anything. The void Print(Node* head) method takes the head node of a linked list as a parameter.


4 Answers

Add a method toString to your LinkedList class

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();
}

Then call it in your main method :

System.out.println(list.toString());
like image 83
yunandtidus Avatar answered Oct 16 '22 02:10

yunandtidus


You have to override the toString() method in your LinkedList<T> class

like image 33
libik Avatar answered Oct 16 '22 02:10

libik


Well, you can either implement the Iterator pattern: http://sourcemaking.com/design_patterns/iterator/java/1

Or you just implement a method that can either print the node element, or execute something on each of them, somewhat like this:

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);
       }
   }
}

Then just do

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

If everything is right, this should work in Java 8.

like image 42
EpicPandaForce Avatar answered Oct 16 '22 04:10

EpicPandaForce


create a getter method in your LinkedList class.

public Node getHead() {

    return head;
}

in your 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.
    }
}  

Hope this works for you.

like image 30
Aditya Singh Avatar answered Oct 16 '22 03:10

Aditya Singh