如何在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;
}
}
}

相关内容

  • 没有找到相关文章

最新更新