Search code examples
javalinked-listnodesuser-inputsingly-linked-list

Singly Linked List ask user n size of node and put an element in each node in java


I can only output it to

12345

54321

how can i ask user to input size of nodes and put elements depending on the size of node

it should be like this:

Sample Output:

Input the number of nodes : 3

Input data for node 1 : 5

Input data for node 2 : 6

Input data for node 3 : 7

Data entered in the list are : 5 6 7

The list in reverse are : 7 6 5

public class LinkedList 
{
    private Node head;
    private Node current;
    
    private static class Node
    {
        private int data;
        private Node next;
        
        public Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
    
    
    public void display()
    {
        Node current = head;
        while (current != null)
        {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println("null");
    }
    
    
    public void reverse()
    {
        Node next = head;
        Node previous = null;
        current = null;
        
        while(next != null)
        {
            current = next;
            next = next.next;
            
            current.next = previous;
            previous = current;
            head = current;
        }
    }
    
    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        
        LinkedList list = new LinkedList();
        
        System.out.print("Eneter number of nodes: ");
        int size = sc.nextInt();
        
        //this is the part where I need a user input
        //for node size and put elements in each nodes

        list.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        Node fourth = new Node(4);
        Node fifth = new Node(5);
        
        list.head.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = fifth;
        
        
        list.display();
        
        list.reverse();
        
        list.display();
        
        
    }
}

Solution

  • You can create an insert method to handle insertion easily

    public class LinkedList {
    
        private static class Node {
            private final int data;
            private Node next;
    
            public Node(int data) {
                this.data = data;
            }
        }
    
        private Node root;
    
        public LinkedList() {
            this.root = null;
        }
    
        public void insert(int value) {
            if (root == null) {
                root = new Node(value);
                return;
            }
    
            Node head = root;
            while (head.next != null) head = head.next;
            head.next = new Node(value);
        }
    
        public void display() {
            Node current = root;
            while (current != null) {
                System.out.print(current.data + " ");
                current = current.next;
            }
            System.out.println();
        }
    
        public void reverse() {
            Node next = root;
            Node previous = null;
            Node current;
    
            while (next != null) {
                current = next;
                next = next.next;
    
                current.next = previous;
                previous = current;
                root = current;
            }
        }
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            LinkedList list = new LinkedList();
    
            System.out.print("Enter number of nodes: ");
            int size = sc.nextInt();
    
            for(int i = 0; i < size ; i++) {
                System.out.print("Input data for node " + i + ": ");
                list.insert(sc.nextInt());
            }
    
            list.display();
            list.reverse();
            list.display();
        }
    }