Search code examples
javalinked-listnodescomputer-science

The node class in linked lists specifically the constructor and using it to create a linked list of random integers


My Node Constructor looks like this:

 public Node(int ndata, Node nlink)
         {
             this.data=ndata; 
             this.link = nlink; 
         }

This constructor takes two parameters, the data for the Node and the link to the next node. For everything I have seen that makes linked lists, however, a new node is created like this:

Node nextNode = new Node (data);

I can't run the program if I don't put the second parameter into the program for some reason, however. This is the code I have.

public static Node ListGenerator()
{
    // Generate RANDOM List
    int j, cint, size;
    Scanner input = new Scanner(System.in); 
    System.out.println("Please enter the size"); 
    size = input.nextInt(); 
    //Node head; 
    Node current = null; 
    for (j = 1; j <= size; j++) {

        cint = (int)((Math.random() * 100)+1); 
        Node nextNode = new Node (cint,current.getLink()); 
        current = nextNode; 

    } return current;     

    // ...  
}

I am new to linked lists so this is very confusing to me even if it is probably a very simple thing I am not getting.


Solution

  • There are a few points to consider in your code:

    1. You'll need a head variable in addition to current to eventually return to the caller (this was commented out, so you're on the right track).

    2. Your first call to current.getLink() will crash because current starts out as null.

    3. This constructor is normal for Node. You can pass null into the second argument as a temporary placeholder for the next node, assuming you have a setter at your disposal to use later. You may wish to overload the constructor to support the link as an optional argument, but it's not necessary if you don't have access to editing the class.

    4. Adding a Scanner inside ListGenerator unnecessarily limits its usage to user input only. This I/O logic is best placed in your main method (or whatever the calling scope is). Further along these lines, consider passing an array of node values in and moving random number generation out of the method as well, further increasing modularity/reusability.

    5. Method names in Java should be lower camelCased.

    6. ANSI C-style variable declarations at the top of the method like int j, cint, size; is generally not used in Java; it's best to declare your index variable inside of the loop scope.

    Here's an approach that begins at the rear of the list and works forward using two pointers to the head and the node immediately following it, next, which is null on the first iteration:

    class Node {
        public int data;
        public Node link;
    
        public Node(int data, Node link) {
            this.data = data; 
            this.link = link; 
        }
    }
    
    class Main {
        public static Node listGenerator(int size) {
            Node next = null;
            Node head = null;
    
            for (int i = 1; i < size; i++) {
                head = new Node((int)(Math.random() * 100) + 1, next);
                next = head;
                head = null;
            }
    
            head = new Node((int)(Math.random() * 100) + 1, next);
            return head;     
        }
    
        public static void main(String[] args) {
            Node head = listGenerator(6);
    
            while (head != null) {
                System.out.print(head.data + "->");
                head = head.link;
            }
    
            System.out.println("null");
        }
    }
    

    Output:

    13->33->87->82->35->87->null
    

    Try it!