im trying to build my own LL, not seeming to work?
My insert method is wrong, and cant fiugre it out, any ideas?

public class List {

private int size;
private Node root;
private Node current;


public List(){
    size = 0;
}

public void insert(int a){

    size++;

    if(root == null){
        root = new Node(a);
        current = root;
    }
    else{

        while(current != null){

            current = current.next;
        }

        current = new Node(a);

    }//else



}//insert

public void o(){

    Node c = root;

    while(c != null){
        System.out.println(c.data);
        c = c.next;
    }

}



private static class Node{

    private int data;
    private Node next;

    public Node(int d){

        data = d;
        next = null;

    }

}//node class


public static void main(String[] args){


    List l = new List();

    l.insert(1);
    l.insert(2);
    l.insert(3);

    l.o();



}//main
}//list class

BTW: this new tagging for code sux

Can you post some output that shows what the code is doing and add some comments to the output to show what it should be. Add enough debugging printlns to the code to show how the variables are set and changed as the code executes.

What is the insert method supposed to do?

Here are some debuging tips. Add a toString to the node class:

    public String toString() {   
       return "Node: data=" + data +", next=" + next;
    }

add this line in the insert method:

System.out.println("insert: root=" + root + "\n   current=" + current);

this code prints 1..
problem is when i insert the next node, 2,
its stays null after i call the constructor..
the problem is the insert method, so im wondering how
it is done correctly

Post the debug print out.

its stays null after i call the constructor..

What stays null?

What is the insert method supposed to do? It lots like waht an add() method would do: add a node at the end of the list. I think of insert as going into a list.

how it is done correctly

Take a piece of paper and work out the logic of adding the first node and then what needs to happen when you add the next node and then the next node etc. You need to connect the links to the nodes as they are added to the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.