單鏈表改為雙向迴圈連結串列
阿新 • • 發佈:2019-01-13
package Linear; /* * 將單鏈表(結點有2個域pre,next,pre為空)改為雙向迴圈連結串列 */ public class I { public static void doubleDirection(Node h) { Node p =h.next; Node pre =h; while (p!=null) { p.pre=pre; p=p.next; pre=pre.next; } pre.next=h; h.pre=pre; } public static void main(String[] args) { Node h=NodeTool.CreateNodeList("abcde"); doubleDirection(h); NodeTool.traverseLoopList(h); System.out.println("---------------------"); Node p=h.pre; while (p!=h) { System.out.println(p.data); p=p.pre; } System.out.println(h.data); } }