简体   繁体   中英

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 when: funA(5-> 2 -> 10 ->3)

Can you see if this works --

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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