java简单链表实现

    xiaoxiao2021-04-11  27

    package testjar; public class Node { private int val; private Node next; public Node() { // TODO Auto-generated constructor stub next=null; } public Node(int val) { // TODO Auto-generated constructor stub this.val=val; next=null; } public int getVal() { return val; } public void setNext(Node next) { this.next=next; } public Node getNext() { return next; } }

    以上代码为节点代码,构造函数要把下个节点指向位置置为空

    package testjar; public class e { public e() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub int[] data={1,2,3,4,5,6,7,8,9,10}; Node p; Node head=new Node(); p=head; for(int i=0;i<data.length;i++){ p.setNext(new Node(data[i])); p=p.getNext(); } p=head.getNext(); for(int i=0;i<data.length;i++){ System.out.println(p.getVal()); p=p.getNext(); } } }

    用数据生成链表,热后再分别打印出来

    转载请注明原文地址: https://ju.6miu.com/read-666805.html

    最新回复(0)