如何通过使用Java中的LinkedList解决此问题?

问题描述

public class LinkedList
{
    Node head;
    public void insert(int data)
    {
        Node node=new Node(data);
        node.data=data;
        head
    }
    public void funA(Node head)
    {
        Node current=head;
        int x=0;
        while(current!=null)
        {
            int data=current.data;
            if(data>3)
            {
                System.out.println(data);
            }
            current=current.nextLine;
        }   
    }
        public static void main(String[] args)
        {
            LinkedList list=new LinkedList();
            list.insert(5);
            list.insert(2);
            list.insert(10);
            list.insert(3);
        }
}

在以下时间输出: funA(5-> 2-> 10-> 3)

解决方法

您能看看是否可行-

public class LinkedList {
    Node head;

    public void insert(int data) {
        if (head == null) {
            head = new Node(data);
        } else {
            Node node = new Node(data);
            Node temp = head;
            while (temp.nextLine != null) {
                temp = temp.nextLine;
            }
            temp.nextLine = node;
        }
    }

    public void funA(Node head) {
        Node current = head;
        int x = 0;
        while (current != null) {
            int data = current.data;
            if (data > 3) {
                System.out.println(data);
            }
            current = current.nextLine;
        }
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(2);
        list.insert(10);
        list.insert(3);
        list.funA(list.head);
    }

    class Node {
        int data;
        Node nextLine;

        public Node(int data) {
            this.data = data;
        }
    }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...