标签:双向 isp 单向链表 etl pack 年龄 gets class span
双向链表与单链表区别在于他多了一个链域,用来存放前驱节点。
基本方法实现:
新增节点:
当前节点的next为新增节点,新增节点的next为当前节点的next.next新增节点的prior为当前结点,当前结点的prior为新增节点
删除节点:
设置删除节点的上一个节点为当前结点。如果当前结点的下下个节点存在的话当前结点的下一个节点为当前结点的next.next,当前结点的下一个节点的prior为当前结点,否则,当前结点的next=null
。
实现:
使用双链表实现学生的新增,删除,输出,查找
创建学生类:student.java
package DoubleLink; /** * @author zh * 学生类 */ public class Student { //学号 private int sId; //姓名 private String sName; //年龄 private int sAge; //班级 private String className; public int getsId() { return sId; } public void setsId(int sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public int getsAge() { return sAge; } public void setsAge(int sAge) { this.sAge = sAge; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public Student(int sId, String sName, int sAge, String className) { this.sId = sId; this.sName = sName; this.sAge = sAge; this.className = className; } @Override public String toString() { return "student{" + "sId=" + sId + ", sName=‘" + sName + ‘\‘‘ + ", sAge=" + sAge + ", className=‘" + className + ‘\‘‘ + ‘}‘; } }
创建节点类:DNode.java
package DoubleLink; /** * @author zh * 双向链表节点 * 与单向链表节点不同的是:双向链表节点多了一个链域,用来存放前驱节点 */ public class DNode { //前驱节点 DNode prior; //数据域 Student student; //后继节点 DNode next; //构造头结点 public DNode(){ this.prior = null; this.student = null; this.next = null; } //构造节点 public DNode(Student student){ this.prior = null; this.student = student; this.next = null; } }
创建链表:DLink.java
package DoubleLink; /** * 双向链表 */ public class DLink { //创建头结点 private DNode head; //初始化 public DLink(){ head = new DNode(); } //新增节点 public void addStudent(Student stu){ DNode temp = head; DNode data = new DNode(stu); while(temp.next != null){ temp = temp.next; } data.prior = temp; temp.next = data; } //有序新增 public void addStudentBySort(Student stu){ DNode temp = head; DNode node = new DNode(stu); while(temp.next != null){ if(node.student.getsAge()<temp.next.student.getsAge()){ temp.next.prior = node; node.next = temp.next; node.prior = temp; temp.next = node; return; } temp = temp.next; } node.prior = temp; temp.next = node; } //由前往后打印双链表 public void displayHeadToLast(){ DNode temp = head; while(temp.next != null){ System.out.println(temp.next.student); temp =temp.next; } } //获取链表最后一个节点 public DNode getLastNodeValue(DLink link){ DNode node = null; DNode temp = link.head; if(link.head.next == null){ return null; } while(temp.next != null){ temp = temp.next; } node = temp; return node; } //删除节点 public void delStudent(String sName){ DNode temp = head; while(temp.next != null){ if(temp.next.student.getsName().equals(sName)){ if(temp.next.next != null){ temp.next.next.prior = temp; temp.next = temp.next.next; }else{ temp.next = null; } break; } temp = temp.next; } } public static void main(String[] args){ DLink dLink = new DLink(); dLink.addStudent(new Student(1,"aa",21,"1")); dLink.addStudent(new Student(1,"bb",20,"2")); dLink.addStudent(new Student(1,"cc",24,"3")); dLink.addStudent(new Student(1,"dd",22,"4")); dLink.addStudentBySort(new Student(1,"aa",21,"1")); dLink.addStudentBySort(new Student(2,"bb",20,"2")); dLink.addStudentBySort(new Student(3,"cc",24,"3")); dLink.addStudentBySort(new Student(4,"dd",22,"4")); dLink.delStudent("cc"); dLink.displayHeadToLast(); System.out.println(); Student stu = dLink.getLastNodeValue(dLink).student; System.out.println(stu); System.out.println("-----------倒序输出------------"); //倒序输出 //获取最后一个节点 DNode node = dLink.getLastNodeValue(dLink); while(node.prior != null){ System.out.println(node.student); node = node.prior; } } }
标签:双向 isp 单向链表 etl pack 年龄 gets class span
原文地址:http://www.cnblogs.com/advanceBlog/p/7900467.html