package A;
import java.util.Stack;
public class Manage1 {
Box head=new Box(0,"");
public void add(Box box){//增
Box temp=head;
while (true){
if (temp.next==null){
break;
}
temp=temp.next;
}
temp.next=box;
box.pre=temp;
}
public void printList(){//打印
Box temp=head.next;
if (temp==null){
System.out.println("empty!");
return;
}
while (true){
if (temp==null){
break;
}
System.out.println(temp);
temp=temp.next;
}
}
public void update(Box box){//改
if (head.next==null){
System.out.println("empty!");
return;
}
Box temp=head.next;
boolean flag=false;
while (true){
if (temp==null){
break;
}
if (temp.id==box.id){
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.name=box.name;
}else {
System.out.println("没有找到");
}
}
public void del(int id){//删
if (head.name==null){
System.out.println("empty!");
return;
}
Box temp=head.next;
boolean flag=false;
while (true){
if (temp==null){
break;
}
if (temp.id==id){
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("no exist!");
}
}
}
package A;
public class Box {
public int id;
public String name;
public Box next;
public Box pre;
public Box(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Box{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
package A;
public class List {
public static void main(String[] args) {
Manage1 manage1=new Manage1();
Box one=new Box(1,"a");
Box two=new Box(2,"ab");
manage1.add(one);
manage1.add(two);
manage1.printList();
System.out.println("=========修改");
Box ome1=new Box(1,"a.a");
manage1.update(ome1);
manage1.printList();
System.out.println("==========删除");
manage1.del(2);
manage1.printList();
}
}
双向链表