问题:L0 L1 L2 L3 Ln--->L0 Ln L1 Ln-1...

  1 import java.util.Scanner;
  2 
  3 /**
  4  * @name: Node
  5  * @description: 问题:L0 L1 L2 L3  Ln--->L0 Ln L1 Ln-1...
  6  * 思路:1.定义指向头的恒定指针headFianl,头指针head 当前指针current,当前指针的前一指针currentP
  7  *      2.将每轮寻得的最后一个元素放头指针head后面
  8  *      3.将头指针head和当前指针current往后一动一次
  9  *      4.重复2-3,直到当前指针current无下一个节点
 10  * 
 11  * @version: v1.0
 12  * @create: 2021-01-30 10:41
 13  **/
 14 
 15 public class NodeTest {
 16     /**单链表形式*/
 17 //    public static void main(String[] args) {
 18 //        Scanner scanner = new Scanner(System.in);
 19 //        System.out.println("Input the number of nodes");
 20 //        int nodesNum = scanner.nextInt();
 21 //        Node[] nodes = new Node[nodesNum];
 22 //        for(int i=0;i<nodesNum;i++) {
 23 //            nodes[i] = new Node();
 24 //        }
 25 //        System.out.println("Input the values of  nodes");
 26 //        for(int i=0;i<nodesNum;i++){
 27 //            nodes[i].value = scanner.nextInt();
 28 //            if(i != nodesNum - 1){
 29 //                nodes[i].right = nodes[i + 1];
 30 //            }
 31 //            System.out.println(nodes[i].toString() + ":" +nodes[i].value);
 32 //        }
 33 //        System.out.println("nodes initial ok");
 34 //
 35 //        Node headFianl = nodes[0];
 36 //        Node head = nodes[0];
 37 //        Node current = head;
 38 //        Node currentP = null;
 39 //        while (current.right != null) {
 40 //            while (current.right != null) {
 41 //                currentP = current;
 42 //                current = current.right;
 43 //            }
 44 //            currentP.right = null;
 45 //            Node tail = current;
 46 //            tail.right = head.right;//重新赋值
 47 //            head.right = tail;
 48 //
 49 //            current = current.right;
 50 //            head = current;
 51 //            if(current == null){
 52 //                break;
 53 //            }
 54 //        }
 55 //
 56 //        System.out.println("output");
 57 //        for(int i=0;i<nodesNum;i++){
 58 //            System.out.println(headFianl.toString() + ":" +headFianl.value);
 59 //            headFianl = headFianl.right;
 60 //        }
 61 //        System.out.println("ok");
 62 //    }
 63 
 64     /**双向链表形式*/
 65     public static void main(String[] args) {
 66         Scanner scanner = new Scanner(System.in);
 67         System.out.println("Input the number of nodes");
 68         int nodesNum = scanner.nextInt();
 69         Node[] nodes = new Node[nodesNum];
 70         for(int i=0;i<nodesNum;i++) {
 71             nodes[i] = new Node();
 72         }
 73         System.out.println("Input the values of  nodes");
 74         for(int i=0;i<nodesNum;i++){
 75             nodes[i].value = scanner.nextInt();
 76             if(i != nodesNum - 1){
 77                 nodes[i].right = nodes[i + 1];
 78                 nodes[i + 1].left = nodes[i];
 79             }
 80             System.out.println(nodes[i].toString() + ":" +nodes[i].value);
 81         }
 82         System.out.println("nodes initial ok");
 83 
 84         Node headFianl = nodes[0];
 85         Node head = nodes[0];
 86         Node current = head;
 87         while (current.right != null) {
 88             while (current.right != null) {
 89                 current = current.right;
 90             }
 91 
 92             current.left.right=null;
 93             Node tail = current;
 94             tail.right = head.right;//重新赋值
 95             if(head.right == null){
 96                 tail.right = head;
 97                 tail.left = head.left;
 98                 tail.left.right = tail;
 99                 head.left = tail;
100                 break;
101             }
102             head.right.left = tail;
103             tail.left = head;
104             head.right = tail;
105 
106             current = current.right;
107             head = current;
108             if(current == null){
109                 break;
110             }
111         }
112 
113         System.out.println("output");
114         for(int i=0;i<nodesNum;i++){
115             System.out.println(headFianl.toString() + ":" +headFianl.value);
116             headFianl = headFianl.right;
117         }
118         System.out.println("ok");
119     }
120 
121     static class Node{
122         Node left;
123         Node right;
124         Object value;
125     }
126 }

 

上一篇:lambda与匿名内部类


下一篇:对于无人驾驶技术(驾驶自动化)L0-L5分级的说明 -转载