繁体   English   中英

如何通过在 java 中使用 LinkedList 来解决这个问题?

[英]how to solve this problem by using LinkedList in java?

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

Output 当: 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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM