java简单代码:
//双向链表 public class DoubleLinkedDemo { public static void main(String[] args) { Student stu1 = new Student(0, "苏珊", "女"); Student stu2 = new Student(1, "琳达", "女"); DoubleLinked linked = new DoubleLinked(); linked.add(stu1); linked.add(stu2); linked.findAll(); linked.delete(0); linked.findAll(); linked.update(new Student(1, "琳达", "boy")); linked.findAll(); } } class DoubleLinked{ private Student head = new Student(0, "", ""); public Student getHead() { return head; } public void setHead(Student head) { this.head = head; } //add public void add(Student stu){ Student temp = head; while(true){ if(temp.next==null){ break; }else{ temp=temp.next; } } temp.next=stu; stu.pre=temp; } //findAll //个人理解(倒序的时候找到最后一个 然后推pre) public void findAll(){ Student temp = head; while(true){ if(temp.next==null && temp.pre==null){ System.out.println("链表中没有数据"); break; } if(temp.next==null){ System.out.println(temp.toString()); break; }else{ if(temp.pre==null){ temp=temp.next; }else{ System.out.println(temp.toString()); temp=temp.next; } } } } //delete public void delete(int scor){ Student temp = head; while(true){ if(temp.next==null && temp.pre==null){ System.out.println("链表中没有数据"); break; } if(temp.pre!=null){ //判断是否是第一个 if(temp.core==scor){ temp.pre.next = temp.next; if(temp.next!=null){ //判断是否是最后一个 temp.next.pre = temp.pre; } break; } } temp=temp.next; } } //update public void update(Student stu){ Student temp = head; while(true){ if(temp.next==null && temp.pre==null){ System.out.println("链表中没有数据"); break; } if(temp.pre!=null){ //判断是否是第一个 if(temp.core==stu.core){ temp.name = stu.name; temp.sex = stu.sex; break; } } temp=temp.next; } } } class Student{ public int core; //排名 public String name; public String sex; public Student pre; //上一个 public Student next;//下一个 public Student(int core, String name, String sex) { this.core = core; this.name = name; this.sex = sex; } @Override public String toString() { return "Student [core=" + core + ", name=" + name + ", sex=" + sex + "]"; } }