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();
}
}