双向链表

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();
}
}

  • 原来妲己是个男的,吓得我西班牙,葡萄牙都掉了,就剩了一个姜子牙还流了一身的成吉思汗,我就用吕布擦了一下,又削了一个马可波罗吃,翻过大桥和小乔,登上了盾山看弈星,我看到了武则天上的赵云,上了一柱孙尚香用狂铁打造了一部8848程咬金手机,默默地发韩信和李信,然后听了一首元歌

posted @ 2021-07-23 12:37  朱在春  阅读(41)  评论(0编辑  收藏  举报