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);
source share