java双向链表的实现

java双向链表的实现

双向链表的增删改查

class DoubleLinkedList {
    private Node head = new Node(0, "head");

    public void deleteNode(Node node) {
        if (isEmpty()) {
            System.out.println("空的");
            return;
        }
        Node tem = head.next;
        boolean flag = false;

        while (true) {
            if (tem == null) {
                break;
            }
            if (node.id == tem.id) {
                flag = true;
                break;
            }
            // tem右移
            tem = tem.next;
        }
        // 此时tem为node同id的节点
        if (flag) {
            // 删除
            tem.pre.next = tem.next;
            // 如果时最后一个节点,不执行,否则空指针异常
            if (tem.next != null) {
                tem.next.pre = tem.pre;
            }
        } else {
            System.out.println("找不到");
        }
    }

    public void update(Node node) {
        if (isEmpty()) {
            System.out.println("空的");
            return;
        }
        Node tem = head.next;
        boolean flag = false;

        while (true) {
            if (tem == null) {
                break;
            }
            if (node.id == tem.id) {
                flag = true;
                break;
            }
            // tem右移
            tem = tem.next;
        }
        if (flag) {
            // 修改
            tem.id = node.id;
            tem.name = node.name;
        } else {
            System.out.println("没找到");
        }
    }

    public void add(Node node) {
        // 找到最后一个节点
        Node tem = head;
        while (true) {
            if (tem.next == null) {
                break;
            }
            // tem右移
            tem = tem.next;
        }
        // tem为队尾
        tem.next = node;
        node.pre = tem;
    }

    public void show() {
        // 如果空
        if (isEmpty()) {
            System.out.println("空的链表");
            return;
        }
        Node tem = head.next;
        while (true) {
            if (tem == null) {
                break;
            }
            System.out.println(tem);
            // tem右移
            tem = tem.next;
        }
    }

    // 辅助方法
    private boolean isEmpty() {
        return (head.next == null);
    }
}

class Node {
    public int id;
    public String name;
    public Node pre;
    public Node next;

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Node(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

测试类

public class Demo {
    public static void main(String[] args) {
        Node node1 = new Node(1, "user01");
        Node node2 = new Node(2, "user02");
        Node node3 = new Node(3, "user03");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(node1);
        doubleLinkedList.add(node2);
        doubleLinkedList.add(node3);

        doubleLinkedList.deleteNode(node1);
        doubleLinkedList.deleteNode(node2);
        doubleLinkedList.update(new Node(3,""));

    }
}
posted @ 2022-03-26 21:56  CoderCatIce  阅读(25)  评论(0编辑  收藏  举报