Java Linked List add Method

Fruit Manatee :

I am attempting to implement a linked list that uses a node class containing head, tail, and current nodes. Part of the linked list is an add method that should add a value to the end of the current node in the list just like an actual linked list would. My issue is that it only works for the first node and then just stops there. For example, in my main I tried testing the code by calling add(1); and add(2);. The console shows me 1 but that's all. I'm unsure if the error is in my add method, toString method, or node class.

I'll also add that I tested whether the correct values were being assigned to "current" in either case, and they were. This has led me to wonder if it's the toString that is the root of the issues, however no matter how much I try I can't change it to make any improvements.

I've hoping fresh eyes may be able to find any blaring issues that may exist.

Add method:

public void add(int val){
    if(current != null){
        Node nextNode = new Node(val, current);
        current = nextNode;
        tail = nextNode;                    
    }
    else{
        head = tail = new Node(val, null);
        current = head;
    }
}

Node class:

public class Node{
    public int data;
    public Node next;

    public Node(int d, Node next) {
        this.data = d;
        this.next = next;
    }
}

toString:

public String toString(){
    for(Node x = head; x != null; x = x.next){
        System.out.println(x.data);
}

All:

public class IntLList extends IntList{    

    public IntLList(){

    }

    public class Node{
        public int data;
        public Node next;

         public Node(int d, Node next) {
            this.data = d;
            this.next = next;
        }
    }

    Node head = null;
    Node tail = null;
    Node current = null;

    public void add(int val){
        if(current != null){
            Node nextNode = new Node(val, current);
            current = nextNode;
            tail = nextNode;    
        }
        else{
            head = tail = new Node(val, null);
            current = head;        
        }
    }

    public int get(int index){
        return 0;
    }

    public void set(int index, int val){
    }

    public void remove(int index) throws ArrayIndexOutOfBoundsException{
    }

    public int size(){
        return 0;
    }

    public String toString(){
        for(Node x = head; x != null; x = x.next){
            System.out.println(x.data);
        }
        return "temp";
    }

    public void removeLast(){
    }

    public boolean isEmpty(){
        boolean isEmpty = false;

        if(head == null){       
            isEmpty = true;
        }

        return isEmpty;
    }

    public void clear(){    
    }

    public static void main(String[] args) {
        IntLList i = new IntLList();
        i.add(1);
        i.add(2);
        i.toString();
    }
}
backdoor :

Make the following changes:

public class Node{

    public int data;
    public Node next;

    public Node(int d, Node next) {

        this.data = d;
        this.next = NULL; // this is to set the next node of current node to null
        if(next!=NULL)
            next.next=this; // this is to set the previous node to point to current node

    }

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=380070&siteId=1