单链表改为双向循环链表

版权声明:转载请说明去处,文章仅供学习参考 https://blog.csdn.net/qq_38487155/article/details/86300905
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);
	}

}

猜你喜欢

转载自blog.csdn.net/qq_38487155/article/details/86300905
今日推荐