双向链表的增删改查

双向链表

package com.baicai.DoubleLinkedList;

public class DoubleLinkedList {
    public static void main(String[] args){

        //定义变量
        Student head = new Student(0,"", null, null);
        Student student1 = new Student(1,"蔡俊杰", null, null);
        Student student2 = new Student(2,"严汉宁", null, null);
        Student student3 = new Student(3,"姚程杰", null, null);
        Student student4 = new Student(4,"周渤森", null, null);
        Student student5 = new Student(5,"金嘉庆", null, null);
        Student student6 = new Student(6,"陈旭瑞", null, null);


        Student student7 = new Student(9,"郑晓聪", null, null);

        Student student8 = new Student(8,"符祥杰", null, null);

        Student student9 = new Student(6,"小黄狗", null, null);


        //建立双向链表
        head.next = student1;
        student1.next = student2;
        student2.next = student3;
        student3.next = student4;
        student4.next = student5;
        student5.next = student6;

        student6.pre = student5;
        student5.pre = student4;
        student4.pre = student3;
        student3.pre = student2;
        student2.pre = student1;
        student1.pre = head;



        //showDoubleLinkedList(head);

        addStudentNode(head, student7);

        //showDoubleLinkedList(head);

        insertStudentNode(head, student8);

        //showDoubleLinkedList(head);

        updateStudentNode(head,student9);

        //showDoubleLinkedList(head);

        deleteStudentNode(head, 9);

        showDoubleLinkedList(head);
    }



class Student{
    public int no;
    public String name;
    public Student next;
    public Student pre;

    public Student(int no,String name,Student pre,Student next){
        this.no = no;
        this.name = name;
        this.next = next;
        this.pre = pre;
    }

    @Override
    public String toString() {
        return no + "    " + name;
    }
}

删除节点的操作

 /*
    *
    * 删除节点
    * */
    public static void deleteStudentNode(Student head,int no){
        //链表为空
        if(head.next == null){
            System.out.println("链表为空,没有办法删除");
            return;
        }

        //定义循环变量
        Student tempStudent = head.next;

        //遍历元素
        while(tempStudent.next != null){
            if(tempStudent.no == no){
                tempStudent.pre.next = tempStudent.next;
                tempStudent.next.pre = tempStudent.pre;
                tempStudent.pre = null;
                tempStudent.next = null;
                break;
            }

            tempStudent = tempStudent.next;
        }

        //最后一个是要删除的节点 
        if(tempStudent.next == null &&  tempStudent.no == no){

           tempStudent.pre.next = null;
           tempStudent.pre = null;
        }

    }

插入节点的操作

    /*插入节点按照编号*/
    public static void insertStudentNode(Student head,Student insertStudentNode){

        if(head.next == null) {
            addStudentNode(head, insertStudentNode);
        }

        //创建临时节点
        Student tempStudent = head.next;

        //辅助变量
        boolean flag = true;

        //遍历节点
        while(flag){

            //插入节点
            if(tempStudent.no > insertStudentNode.no){
                insertStudentNode.pre = tempStudent.pre;
                insertStudentNode.next = tempStudent.pre.next;
                tempStudent.pre.next = insertStudentNode;
                tempStudent.pre = insertStudentNode;
                flag = false;
            }

            if(tempStudent.next == null || flag == false){
                break;
            }

            tempStudent = tempStudent.next;
        }

        //如果前面没有合适的插入的节点,就直接放在最后面
        if(flag == true){
            addStudentNode(head, insertStudentNode);
        }


    }

}

正向遍历节点的操作

/*正向遍历列表*/
    public static void showDoubleLinkedList(Student head){
        if(head == null || head.next == null){
            System.out.println("链表为空或者链表头节点为空");
            return;
        }

        //临时节点
        Student tempStudentNode = head.next;

        //循环遍历
        while(tempStudentNode != null){
            //打印节点
            System.out.println(tempStudentNode);

            //指向下一个元素
            tempStudentNode = tempStudentNode.next;
        }

    }

修改节点的操作

*修改节点  */
    public static void updateStudentNode(Student head,Student studentNode){

        if(head.next == null){
            System.out.println("链表为空,无法完成修改元素");
            return;
        }

        Student tempStudent = head.next;

        //遍历链表查看有没有相同的元素
        while(tempStudent.next != null) {
            if (studentNode.no == tempStudent.no) {
                tempStudent.name = studentNode.name;
            }


            tempStudent = tempStudent.next;
        }
    }

 

在链表的最后添加节点

/* 在链表的最后添加节点 */
    public static void addStudentNode(Student head,Student studentNode){
        if(head.next == null){
            head.next = studentNode;
            studentNode.pre = head;
            return;
        }

        //建立临时节点
        Student tempStudent = head.next;

        //遍历节点
        while(tempStudent.next != null){
            //遍历下一个节点
            tempStudent = tempStudent.next;
        }

        //插入节点
        tempStudent.next = studentNode;
        studentNode.pre = tempStudent;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值