importjava.util.Arrays;importjava.util.Scanner;publicclassNodeUse{// 头插法publicstaticNodeXheadSert(int[] arr){NodeX head =null;for(int i =0; i < arr.length; i++){NodeX q =newNodeX(arr[i]);
q.next = head;
head = q;}return head;}// 尾插法publicstaticNodeXtailSert(int[] arr){NodeX head =newNodeX();NodeX p = head;for(int i =0; i < arr.length; i++){NodeX q =newNodeX(arr[i]);
p.next = q;
q.next =null;
p = q;}return head.next;}// 逆序publicstaticNodeXreserseNode(NodeX head){NodeX p = head.next;NodeX q = head;NodeX w;
q.next =null;while(p !=null){
w = p.next;
p.next = q;
q = p;
p = w;}return q;}publicstaticvoidmain(String[] args){// 进行实际操作Scanner in =newScanner(System.in);System.out.println("请输入个数:");int num = in.nextInt();int[] arr =newint[num];System.out.println("输入数组:");for(int i =0; i < num; i++){
arr[i]= in.nextInt();}System.out.println(Arrays.toString(arr));NodeX head =headSert(arr);System.out.println("头插法:"+ head.toString());
head =null;
head =tailSert(arr);System.out.println("尾插法:"+ head.toString());
head =reserseNode(head);System.out.println("逆序:"+ head.toString());}}classNodeX{int val;NodeX next;publicNodeX(){}publicNodeX(int val){this.val = val;}publicStringtoString(){if(next ==null)return val +"";return val +" -> "+ next.toString();}}