如何使用Java中的链表解决这个问题?

huangapple go评论80阅读模式
英文:

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;
        // 省略插入逻辑部分
    }
    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.next; // 修正为 current.next
        }
    }
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(2);
        list.insert(10);
        list.insert(3);
    }
}
英文:
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)

答案1

得分: 1

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;
        }
    }
}
英文:

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

huangapple
  • 本文由 发表于 2020年8月13日 18:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63393208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定