天天看點

雙向循環連結清單--Java實作

package cn.CSDN.Ravanla;

public class DoubleLink {
	public static void main(String[] args) {
		doubleNode n1 = new doubleNode(1);
		doubleNode n2 = new doubleNode(2);
		doubleNode n3 = new doubleNode(3);
		doubleNode n4 = new doubleNode(4);
		
		n1.after(n2);
		n2.after(n3);
		n3.after(n4);
		
		n1.searchAll();
	}
}
           
class doubleNode{
	doubleNode pre = this;
	doubleNode next = this;;
	int data;
	public doubleNode(int data) {
		this.data = data;
	}
           
public void after(doubleNode node) {
		doubleNode nextNext = next;
		this.next = node;
		node.pre = this;
		node.next = nextNext;
		nextNext.pre = node;
	}
           
雙向循環連結清單--Java實作

前面那個圖檔畫錯了pre和next指的對象指錯了,不過還是可以和下面的圖連起來看的

雙向循環連結清單--Java實作
我用畫圖工具尴尬地畫出了我所了解的,希望對你有用。
           
public int getData(doubleNode node) {
		return node.data;
	}
	public doubleNode getNext(doubleNode node) {
		return node.next;
	}
           
public void searchAll() {
		doubleNode temp = this;
		int value = this.data;
		while(temp.next.data != value) {
			System.out.println(temp.data);
			temp = temp.next;
		}
		System.out.println(temp.data);
	}
}
           

繼續閱讀