双链表的实现、CRUD操作、顺序插入、顺序合并链表

因为是复习,所以直接贴代码

package com.leolee.dataStructure.linkedList;

/**
 * @ClassName BidirectionNode
 * @Description: 双向链表节点
 * @Author LeoLee
 * @Date 2020/9/14
 * @Version V1.0
 **/
public class BidirectionNode {

    public int no;

    public String name;

    public String nikeName;

    public BidirectionNode next;

    public BidirectionNode pre;

    public BidirectionNode (int no, String name, String nikeName) {
        this.no = no;
        this.name = name;
        this.nikeName = nikeName;
    }

    @Override
    public String toString() {
        return "BidirectionNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nikeName='" + nikeName + '\'' +
                '}';
    }
}
package com.leolee.dataStructure.linkedList;

/**
 * @ClassName BidirectionLinkedList
 * @Description: 双向链表
 * @Author LeoLee
 * @Date 2020/9/14
 * @Version V1.0
 **/
public class BidirectionLinkedList {

    //初始化头节点
    private BidirectionNode headNode = new BidirectionNode(0, "", "");

    private int size;

    //遍历链表
    public void list () {

        if (headNode.next == null) {
            System.out.println("链表为空");
            return;
        }

        BidirectionNode tempNode = headNode.next;
        while (true) {
            if (tempNode == null) {
                break;
            }
            //输出节点信息
            System.out.println(tempNode.toString());
            //节点后移
            tempNode = tempNode.next;
        }
    }

    //在链表尾部插入节点
    public void add (BidirectionNode newNode) {

        BidirectionNode tempNode = headNode;

        //遍历寻找最后的节点
        while (true) {
            if (tempNode.next == null) {
                break;
            }
            tempNode = tempNode.next;
        }
        tempNode.next = newNode;
        newNode.pre = tempNode;
        size++;
    }

    public void addByOrder (BidirectionNode newNode) {

        BidirectionNode tempNode = headNode.next;
        boolean flag = false;//相同编号的节点是否已经存在
        while (true) {
            if (tempNode == null) {
                break;
            }
            if (newNode.no > tempNode.no) {
                break;
            }
            if (newNode.no == tempNode.no) {
                flag = true;
                break;
            }
            tempNode = tempNode.next;
        }

        if (flag) {
            System.out.printf("添加的节点编号已经存在%d,不能添加\n", newNode.no);
        } else {
            newNode.pre = tempNode;
            tempNode.next = newNode;
            size++;
        }
    }

    public void update (BidirectionNode newNode) {

        //判断链表是否为空
        if (headNode.next == null) {
            System.out.println("链表为空");
            return;
        }
        BidirectionNode tempNode = headNode.next;
        boolean flag = false;//是否找到要修改的节点(是否找到no一样的节点)
        while (true) {
            if (tempNode == null) {//最后一个节点的next为空,遍历结束了
                break;
            }
            if (tempNode.no == newNode.no) {
                flag = true;
                break;
            }
            tempNode = tempNode.next;
        }

        if (flag) {
            tempNode.name = newNode.name;
            tempNode.nikeName = newNode.nikeName;
        } else {
            System.out.printf("没有找到对应需要修改的节点[%d]", newNode.no);
        }
    }

    public void delete (int targetNo) {

        if (headNode.next == null) {
            System.out.println("链表为空,无法删除");
            return;
        }

        BidirectionNode tempNode = headNode.next;
        boolean flag = false;//是否找到需要删除的节点的上一个节点
        while (true) {
            if (tempNode == null) {
                System.out.println("已经遍历到了最后节点");
                break;
            }
            if (tempNode.no == targetNo) {
                flag = true;
                break;
            }
            tempNode = tempNode.next;
        }

        if (flag) {
            tempNode.pre.next = tempNode.next;
            if (tempNode.next != null) {
                tempNode.next.pre = tempNode.pre;
            }
            size--;
        }
    }

    public BidirectionNode getHeadNode() {
        return headNode;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25805331/article/details/108584776