1.结点
package com.example;
import java.util.Objects;
public class DoubleNode {
private Integer id;
private String name;
private DoubleNode preNode;
private DoubleNode nextNode;
public DoubleNode() {
}
public DoubleNode(Integer id, String name, DoubleNode preNode, DoubleNode nextNode) {
this.id = id;
this.name = name;
this.preNode = preNode;
this.nextNode = nextNode;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DoubleNode that = (DoubleNode) o;
return Objects.equals(id, that.id) &&
Objects.equals(name, that.name) &&
Objects.equals(preNode, that.preNode) &&
Objects.equals(nextNode, that.nextNode);
}
@Override
public int hashCode() {
return Objects.hash(id, name, preNode, nextNode);
}
@Override
public String toString() {
return "DoubleNode{" +
"id=" + id +
", name='" + name +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DoubleNode getPreNode() {
return preNode;
}
public void setPreNode(DoubleNode preNode) {
this.preNode = preNode;
}
public DoubleNode getNextNode() {
return nextNode;
}
public void setNextNode(DoubleNode nextNode) {
this.nextNode = nextNode;
}
}
2.链表
package com.example;
public class DoubleLinkList {
private static DoubleNode headNode = new DoubleNode(1,"头结点",null,null);
public void list(){
if (headNode.getNextNode()==null){
System.out.println("链表为空");
return;
}
DoubleNode flag = headNode;
while (true){
if (flag.getNextNode()!=null){
System.out.println(flag.getNextNode());
flag = flag.getNextNode();
}else{
break;
}
}
}
public String addNodeToEnd(DoubleNode doubleNode){
DoubleNode flag = headNode;
while (true){
if(flag.getNextNode()== null){
flag.setNextNode(doubleNode);
doubleNode.setPreNode(flag);
break;
}
flag = flag.getNextNode();
}
return "添加成功";
}
public String deleteNode(DoubleNode doubleNode){
DoubleNode flag = headNode;
while (true){
if (flag.getId() == doubleNode.getId()){
if (flag.getNextNode()==null){
flag.getPreNode().setNextNode(null);
flag.setPreNode(null);
break;
}else{
flag.getPreNode().setNextNode(flag.getNextNode());
flag.getNextNode().setPreNode(flag.getPreNode());
break;
}
}
if (flag.getNextNode()==null){
return "删除失败,没有数据";
}
flag = flag.getNextNode();
}
return "删除成功";
}
}
3.测试类
package com.example;
public class Test {
public static void main(String[] args){
DoubleNode doubleNode1 = new DoubleNode(2,"张三",null,null);
DoubleNode doubleNode2 = new DoubleNode(3,"李四",null,null);
DoubleNode doubleNode3 = new DoubleNode(4,"王五",null,null);
DoubleNode doubleNode4 = new DoubleNode(5,"赵六",null,null);
DoubleNode doubleNode5 = new DoubleNode(6,"冯七",null,null);
DoubleNode doubleNode7 = new DoubleNode(7,"冯8",null,null);
DoubleLinkList doubleLinkList = new DoubleLinkList();
doubleLinkList.addNodeToEnd(doubleNode1);
doubleLinkList.addNodeToEnd(doubleNode2);
doubleLinkList.addNodeToEnd(doubleNode3);
doubleLinkList.addNodeToEnd(doubleNode4);
doubleLinkList.addNodeToEnd(doubleNode5);
doubleLinkList.list();
System.out.println("------------------------------------");
String s = doubleLinkList.deleteNode(doubleNode5);
System.out.println(s);
doubleLinkList.list();
System.out.println("------------------------------------");
String s1 = doubleLinkList.deleteNode(doubleNode2);
System.out.println(s1);
doubleLinkList.list();
System.out.println("------------------------------------");
String s2 = doubleLinkList.deleteNode(doubleNode7);
System.out.println(s2);
doubleLinkList.list();
}
}