/**
* 双向链表
*/
public class DoubleLinkListDemo {
public static void main(String[] args){
DoubleLinkList linkList = new DoubleLinkList();
DoubleNode n1 = new DoubleNode(1,"A");
DoubleNode n2 = new DoubleNode(2,"B");
DoubleNode n3 = new DoubleNode(3,"C");
DoubleNode n4 = new DoubleNode(4,"D");
linkList.add(n1);
linkList.add(n2);
System.out.println("add----");
linkList.list();
DoubleNode n11 = new DoubleNode(1,"A~");
linkList.update(n11);
System.out.println("update----");
linkList.list();
linkList.del(1);
System.out.println("del----");
linkList.list();
System.out.println("-------------");
DoubleLinkList linkList2 = new DoubleLinkList();
DoubleNode n12 = new DoubleNode(1,"A");
DoubleNode n22 = new DoubleNode(2,"B");
DoubleNode n32 = new DoubleNode(3,"C");
DoubleNode n42 = new DoubleNode(4,"D");
linkList2.addOrder(n22);
linkList2.addOrder(n32);
linkList2.addOrder(n12);
linkList2.addOrder(n42);
System.out.println("add----");
linkList2.list();
DoubleNode n111 = new DoubleNode(1,"A~");
linkList2.update(n111);
System.out.println("update----");
linkList2.list();
linkList2.del(1);
System.out.println("del----");
linkList2.list();
}
//add----
//DoubleNode{no=1, data='A', next=2, pre=0}
//DoubleNode{no=2, data='B', next=null, pre=1}
//update----
//DoubleNode{no=1, data='A~', next=2, pre=0}
//DoubleNode{no=2, data='B', next=null, pre=1}
//del----
//DoubleNode{no=2, data='B', next=null, pre=0}
//-------------
//add----
//DoubleNode{no=1, data='A', next=2, pre=0}
//DoubleNode{no=2, data='B', next=3, pre=1}
//DoubleNode{no=3, data='C', next=4, pre=2}
//DoubleNode{no=4, data='D', next=null, pre=3}
//update----
//DoubleNode{no=1, data='A~', next=2, pre=0}
//DoubleNode{no=2, data='B', next=3, pre=1}
//DoubleNode{no=3, data='C', next=4, pre=2}
//DoubleNode{no=4, data='D', next=null, pre=3}
//del----
//DoubleNode{no=2, data='B', next=3, pre=0}
//DoubleNode{no=3, data='C', next=4, pre=2}
//DoubleNode{no=4, data='D', next=null, pre=3}
}
class DoubleLinkList{
//头结点
private DoubleNode head = new DoubleNode(0,"");
public DoubleNode getHead() {
return head;
}
private boolean addMethod = false;
//顺序插入
public void add(DoubleNode node){
this.addMethod = true;
DoubleNode temp = head;
//找尾结点
while (true){
if (temp.next==null){
break;
}
temp = temp.next;
}
temp.next = node;
node.pre = temp;
}
//有序插入 根据no
public void addOrder(DoubleNode node){
if (addMethod){
System.out.println("有序插入不能与add()调用后使用!");
return;
}
DoubleNode temp = head;
boolean flag = false; //重复的no
while (true){
if (temp.next==null){
break;
}else if (temp.next.no == node.no){
flag = true;
System.out.println("存在重复的Node");
break;
}else if (temp.next.no > node.no){
break;
}
temp = temp.next;
}
if (!flag){
node.next = temp.next;
temp.next = node;
temp.next.pre = temp;
if (node.next!=null){
node.next.pre = node;
}
}
}
//修改 根据no 修改data
public void update(DoubleNode node){
if (head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head.next;
boolean flag = false; //是否存在该节点
while (true){
if (temp == null){
break;
}
if (temp.no == node.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.data = node.data;
}else {
System.out.println("不存在该结点,不能修改");
}
}
//根据no删除结点
public void del(int no){
if (head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head.next;
boolean flag = false; //是否存在该结点
while (true){
if (temp == null){
break;
}
if (temp.no == no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.pre.next = temp.next;
if (temp.next!=null){
temp.next.pre = temp.pre;
}
}else {
System.out.println("不存在该结点,不能删除");
}
}
//显示单链表
public void list(){
if (head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head.next;
while (true){
if (temp!=null){
System.out.println(temp);
temp = temp.next;
}else {
break;
}
}
}
}
class DoubleNode{
public int no;
public String data;
public DoubleNode next;
public DoubleNode pre;
public DoubleNode(int no, String data) {
this.no = no;
this.data = data;
}
@Override
public String toString() {
if (next!=null){
return "DoubleNode{" +
"no=" + no +
", data='" + data + '\'' +
", next=" + next.no +
", pre=" + pre.no +
'}';
}else {
return "DoubleNode{" +
"no=" + no +
", data='" + data + '\'' +
", next=null" +
", pre=" + pre.no +
'}';
}
}
}