How to print data on LinkedList

I have success created by LinkedList from scratch. For now, this can only add data. There was no deletion or anything like that.

I can add strings, integers, etc., but I had a problem printing the data I added. How can I do it? I probably have to skip 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 the main thing. Where I create an object from a Linkedlist and use the general add method to add my data. But how to print it 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 
+5
source share
4 answers

Add toString Method to 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 the main method:

 System.out.println(list.toString()); 
+6
source

You must override the toString() method in the LinkedList<T> class

+1
source

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

Or, you simply implement a method that can either print a node element or execute something on each of them, something 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 correct, this should work in Java 8.

+1
source

create a getter method in your LinkedList class.

 public Node getHead() { return head; } 

in 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, ie 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.

+1
source

Source: https://habr.com/ru/post/1204403/


All Articles