双向链表
简介
它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
代码
节点对象
class Student2 { public int stuNo; public String name; //指向下一个节点 public Student2 next; //指向上一个节点 public Student2 prev; public Student2() { } public Student2(int stuNo, String name) { this.stuNo = stuNo; this.name = name; } @Override public String toString() { return "Student2{" + "stuNo=" + stuNo + ", name='" + name + '\'' + '}'; } }
不考虑编号排序,直接添加
class DoubleLinkedList { //初始化头节点,头节点不能动,不存放具体的数据 private final Student2 head = new Student2(0,""); public Student2 getHead() { return head; } /** * 不考虑编号顺序,直接添加到最后 * @param stu 添加的节点对象 */ public void add(Student2 stu){ Student2 temp = head; while (temp.next != null) { temp = temp.next; } stu.prev = temp; temp.next = stu; } /** * 显示所有节点 */ public void show(){ if(head.next == null){ return; } Student2 temp = head.next; while (temp != null){ System.out.println(temp); temp = temp.next; } } }
Student2 stu1 = new Student2(1,"1号"); Student2 stu2 = new Student2(2,"2号"); Student2 stu3 = new Student2(3,"3号"); Student2 stu4 = new Student2(4,"4号"); DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(stu1); doubleLinkedList.add(stu2); doubleLinkedList.add(stu4); doubleLinkedList.add(stu3); doubleLinkedList.show();
按照编号排序添加
/** * @param stu 要添加的节点对象 * 根据编号排序添加 * 如果要添加的节点已经存在,给出提示 */ public void addByNo(Student2 stu){ Student2 temp = head; //标识要添加的学生是否存在 boolean flag = false; while (temp.next != null){ if(temp.next.stuNo == stu.stuNo){ flag = true; break; } if(temp.next.stuNo > stu.stuNo){ break; } temp = temp.next; } if(flag){ System.out.printf("编号为%d的学生已经存在\n",stu.stuNo); }else { stu.next = temp.next; if(temp.next != null){ temp.next.prev = stu; } temp.next = stu; stu.prev = temp; } }
doubleLinkedList.addByNo(stu1);
doubleLinkedList.addByNo(stu2);
doubleLinkedList.addByNo(stu4);
doubleLinkedList.addByNo(stu3);
doubleLinkedList.addByNo(stu3);
doubleLinkedList.show();
修改
/** * * @param stu 要修改的学生对象 */ public void update(Student2 stu){ if(head.next == null){ System.out.println("链表无数据"); return; } boolean flag = false; Student2 temp = head; while (temp != null){ if(temp.stuNo == stu.stuNo){ flag = true; break; } temp = temp.next; } if(flag){ temp.name = stu.name; }else { System.out.printf("编号为%d的学生不存在\n",stu.stuNo); } }
doubleLinkedList.addByNo(stu1); doubleLinkedList.addByNo(stu2); doubleLinkedList.addByNo(stu4); doubleLinkedList.addByNo(stu3); doubleLinkedList.show(); Student2 stu = new Student2(4,"00"); doubleLinkedList.update(stu); System.out.println("修改后的数据"); doubleLinkedList.show();
删除
/** * @param stuNo 根据学生编号删除 */ public void delete(int stuNo){ if(head.next == null){ System.out.println("链表为空"); return; } Student2 temp =head.next; //标识要删除的学生是否存在 boolean flag = false; while (temp != null){ if(temp.stuNo == stuNo){ flag = true; break; } temp = temp.next; } if(flag){ temp.prev.next = temp.next; //如果要删除的对象不是最后一个节点 if(temp.next != null){ temp.prev = temp.next.prev; } }else { System.out.printf("编号为%d的学生不存在\n",stuNo); } }
doubleLinkedList.delete(4); doubleLinkedList.delete(1); doubleLinkedList.delete(1); System.out.println("删除后的数据=============="); doubleLinkedList.show();