数据结构复习(7)---双向循环链表

上一章:数据结构复习(6)—循环链表

package cjy.datastructure;
/**
 * 双向循环链表
 * Title: DoubleNode.java
 * @author CJY》10258
 * @date 2019年7月12日
 */
public class DoubleNode {
	//数据域
	int data;
	//双向循环 ,前驱 ,后继 = this 循环
	DoubleNode pre = this;
	DoubleNode next = this;

	public DoubleNode(int data) {
		this.data = data;
	}

	public int getData() {
		return this.data;
	}

	public DoubleNode pre() {
		return this.pre;
	}

	public DoubleNode next() {
		return this.next;
	}

	/**
	 * 添加数据
	 * 
	 * @param newNode
	 */
	public void insertDoubleNode(DoubleNode newNode) {
		//拿下一个指针
		DoubleNode nextNode = next;
		//新指针指向原来的
		this.next = newNode;
		newNode.pre = nextNode;
		newNode.next = nextNode;
		nextNode.pre = newNode;
	}
	/**
	 * 删除
	 */
	public void deleteDoubleNode() {
		// 1 - 2 - 3   拿到1  》》3   和 3》》1的指针 
		DoubleNode nextNextNode = next.next;
		DoubleNode prePreNode = nextNextNode.pre.pre;
		//3》》1   重新指向1
		this.next = nextNextNode;
		this.pre = prePreNode;
		//1》》3   重新指向3
		nextNextNode = this.next;
		prePreNode = this.pre;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41367523/article/details/95663432
今日推荐