在上一篇循环链表的基础上给没个节点新增前继节点,代码如下,相对于循环链表的几点主要不同之处用红色字体标出,本文同样贴出主要代码以及运行结果,测试代码在之前单链表的实现有给出此处不再列出。
package cn.dtstructure;
public class DoubleLoopLinkList<E> {
private Node head_point;//头指针
private Node head_node;//头节点
private Node rear;//尾指针
private Node point;//临时指针
private Integer length;//长度
private class Node{
private E data;//数据域
//1.新增前继指针
private Node next;//后继指针
private Node front;//前继指针
//未带参数的构造函数用于初始化头结点
public Node(){
this.data = null;
}
//带参数的构造函数用于初始化节点数据
public Node(E data){
this.data = data;
}
}
//初始化链表 只创建不含数据的头结点
public DoubleLoopLinkList(){
this.head_point = new Node();
this.head_node = new Node();
this.rear = head_node;
//2.前继指针以及后继指针都指向都节点
head_node.front = head_node;
head_node.next = head_node;
this.length = 0;
System.out.println("链表进行初始化");
show();
}
//从尾部添加节点 传入数据
public void insert(E data){
//临时指针指向新建节点
point = new Node(data);
//尾指针的下一指针指向新节点
rear.next = point;
//3.新增节点的前继指针指向前一个节点
rear.next.front = rear;
//尾指针指向新节点
rear = point;
//4.头结点的前继指针指向最后一个节点
head_node.front = point;
//最后一个节点指向头结点
point.next = head_node;
// point.next = null;
length++;
}
//在指定位置添加节点 默认第一个节点位置为1
public void insertInPosition(E data,Integer position){
if (position>=1 && position<=length) {
//在第一个节点到倒数第二个节点间添加新节点
//将临时指针移动到操作位置的前一个位置
move(position);
Node tmp = new Node(data);
//5.需要新增被移动位置的节点和新增节点的前继指针 建议画画图看着比较明白
point.next.front = tmp;
tmp.next = point.next;
tmp.front = point;
point.next = tmp;
length++;
}else if (position == length+1) {
//在最后一个位置添加节点
insert(data);
}else {
System.out.println("插入位置不正确");
}
}
//删除指定位置节点
public void delete(Integer position){
if (position>=1 && position<=length) {
//将临时指针移动到操作位置的前一个位置
move(position);
//若是删除最后一个节点 则point.next = null;
point.next = point.next.next;
//6.修改被删除元素的后继元素的前继指针
point.next.front = point;
//判断最后一个节点是否改变 若改变移动尾指针到对应位置
if (point.next == null) {
rear = point;
//若是删除最后一个节点 则更新后链表的最后一个节点指向头结点
point.next = head_node;
//7.头结点的前继指针指向最后一个节点
head_node.front = point;
}
length--;
}else{
System.out.println("删除位置不正确");
}
}
//修改指定位置节点
public void update(Integer position,E data){
if (position>=1 && position<=length) {
//将临时指针移动到操作位置的前一个位置
move(position);
point.next.data = data;
}else{
System.out.println("更新位置不正确");
}
}
//查询指定位置节点
public void select(Integer position){
if (position>=1 && position<=length) {
//将临时指针移动到操作位置的前一个位置
move(position);
System.out.println("当前位置节点的数据为:"+point.next.data);
}else{
System.out.println("查询位置不正确");
}
}
//显示链表当前信息
public void show(){
System.out.println("*********后继指针遍历链表*********");
String result = "head_point->head_node->";
point = head_node;
while(point.next!=head_node){
result = result + point.next.data + "->";
point = point.next;
}
result += "head_node";
System.out.println(result);
System.out.println("*********前继指针遍历链表*********");
result = "head_point->head_node->";
point = head_node;
while(point.front!=head_node){
result = result + point.front.data + "->";
point = point.front;
}
result += "head_node";
System.out.println(result);
}
//将临时指针移动到带操作节点的前一个位置
public void move(Integer position){
point = head_node;
while(--position>0){
point = point.next;
}
}
}
运行结果
链表进行初始化
*********后继指针遍历链表*********
head_point->head_node->head_node
*********前继指针遍历链表*********
head_point->head_node->head_node
insert(2)
*********后继指针遍历链表*********
head_point->head_node->2->head_node
*********前继指针遍历链表*********
head_point->head_node->2->head_node
insert(3)
*********后继指针遍历链表*********
head_point->head_node->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->head_node
insert(5)
*********后继指针遍历链表*********
head_point->head_node->2->3->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->3->2->head_node
insertInPosition(1, 1)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->3->2->1->head_node
insertInPosition(4, 4)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->4->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->4->3->2->1->head_node
insertInPosition(6, 6)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->4->5->6->head_node
*********前继指针遍历链表*********
head_point->head_node->6->5->4->3->2->1->head_node
delete(1)
*********后继指针遍历链表*********
head_point->head_node->2->3->4->5->6->head_node
*********前继指针遍历链表*********
head_point->head_node->6->5->4->3->2->head_node
delete(3)
*********后继指针遍历链表*********
head_point->head_node->2->3->5->6->head_node
*********前继指针遍历链表*********
head_point->head_node->6->5->3->2->head_node
delete(4)
*********后继指针遍历链表*********
head_point->head_node->2->3->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->3->2->head_node
update(1, 1)
*********后继指针遍历链表*********
head_point->head_node->1->3->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->3->1->head_node
update(2, 2)
*********后继指针遍历链表*********
head_point->head_node->1->2->5->head_node
*********前继指针遍历链表*********
head_point->head_node->5->2->1->head_node
update(3, 3)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->1->head_node
当前位置节点的数据为:1
select(1)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->1->head_node
当前位置节点的数据为:2
select(2)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->1->head_node
当前位置节点的数据为:3
select(3)
*********后继指针遍历链表*********
head_point->head_node->1->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->1->head_node
select(4)
查询位置不正确
insertInPosition(5, 5)
插入位置不正确
delete(4)
删除位置不正确
update(0, 0)
更新位置不正确
*****最后结果*****
*********后继指针遍历链表*********
head_point->head_node->1->2->3->head_node
*********前继指针遍历链表*********
head_point->head_node->3->2->1->head_node