package com.数据结构;
public class 双向链表 {
public static void main(String[] args){
Node2 node1 = new Node2(1,"宋江","及时雨");
Node2 node2 = new Node2(5,"林冲","豹子头");
Node2 node3 = new Node2(1,"宋江","及时雨");
Node2 node4 = new Node2(2,"吴用","智多星");
Node2 node5 = new Node2(4,"吴用","智多星");
Node2 node6 = new Node2(9,"吴用","智多星");
LinkedTwo linked = new LinkedTwo();
linked.sortAdd(node1);
linked.sortAdd(node2);
linked.sortAdd(node4);
linked.sortAdd(node3);
linked.sortAdd(node5);
linked.sortAdd(node6);
linked.pritf();
linked.del(new Node2(5," "," "));
linked.del(new Node2(3," "," "));
Node2 node66= new Node2(1,"狗子","666");
linked.mod(node66);
linked.pritf();
System.out.println("----------------------");
linked.seek(1);
System.out.println("----------------------");
linked.seek(-1);
}
}
class LinkedTwo{
public Node2 head = new Node2();
public void add(Node2 newNode){
Node2 temp = head;
while(temp.next!=null){
temp = temp.next ;
}
temp.next = newNode ;
newNode.pre = temp;
}
public void sortAdd(Node2 newNode){
Node2 temp = head;
boolean flag = false;
while(true){
if(temp.next==null||temp.next.no>newNode.no){
flag = true ;
break;
}else if(temp.next.no==newNode.no){
break;
}
temp = temp.next;
}
if(flag){
newNode.next = temp.next ;
newNode.pre = temp ;
if(temp.next!=null){
temp.next.pre=newNode ;
}
temp.next = newNode ;
}else{
System.out.println("数据已经存在~~~");
}
}
public void del(Node2 newNode){
Node2 temp = head.next;
boolean flag = false ;
while(true){
if(temp==null){
break;
}else if(newNode.no==temp.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.pre.next=temp.next ;
if(temp.next!=null){
temp.next.pre=temp.pre;
}
System.out.println("删除成功 剩余数据:");
pritf();
}else{
System.out.println("数据不存在 删除失败~~~");
}
}
public void mod(Node2 newNode){
Node2 temp = head.next;
boolean flag = false ;
while(temp!=null){
if(temp.no==newNode.no){
flag = true;
break;
} temp = temp.next ;
}
if(flag){
newNode.next = temp.next ;
if(temp.next!=null){
temp.next.pre = newNode;
}
newNode.pre = temp.pre;
temp.pre.next = newNode ;
}
}
public void seek(int noo){
Node2 temp = head.next ;
boolean flag = false ;
while(temp!=null){
if(temp.no==noo){
flag = true ;
break;
}
temp=temp.next;
}
if(flag){
System.out.println(temp);
}else{
System.out.println("数据不存在~~~");
}
}
public void pritf(){
Node2 temp = head.next;
while(temp!=null){
System.out.println(temp);
temp = temp.next ;
}
}
}
class Node2{
public int no ;
public String name ;
public String id ;
public Node2 pre ;
public Node2 next ;
public Node2() {
}
public Node2(int no, String name, String id) {
this.no = no;
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Node2{" +
"no=" + no +
", name='" + name + '\'' +
", id='" + id + '\'' +
'}';
}
}