单向链表询问用户 n 个节点的大小,并在 java 中的每个节点中放入一个元素

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

我只能输出到

12345

54321

我如何要求用户输入节点的大小 并根据节点的大小放置元素

应该是这样的:

示例输出:

输入节点数:3

节点 1 的输入数据:5

节点 2 的输入数据:6

节点 3 的输入数据:7

列表中输入的数据是:5 6 7

倒过来的名单是: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();
        
        
    }
}

您可以创建一个插入方法来轻松处理插入

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