java資料結構:雙向鏈表

來源:互聯網
上載者:User

標籤:雙向   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;        }    }}

 

java資料結構:雙向鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.