import java.util.Scanner;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
public class MYLinkedList {
Scanner sc = new Scanner(System.in);
int data;
Node head = new Node(0);
public void createLinkedList() {
head.next = null;
System.out.println("请输入你要插入的数据:到0结束");
data = sc.nextInt();
while (data != 0) {
Node node = new Node(data);
node.next = head.next;
head.next = node;
data = sc.nextInt();
}
}
public void printfLinkedList() {
Node temp = new Node(0);
temp = head.next;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public static void main(String args[]) {
MYLinkedList link = new MYLinkedList();
link.createLinkedList();
link.printfLinkedList();
}
}
java的链表
最新推荐文章于 2023-09-23 20:18:55 发布