0
点赞
收藏
分享

微信扫一扫

单向数据链表的具体实现

E_topia 2021-09-21 阅读 63
日记本

话不说上代码

public class SingleLinkedList {

    private int size; // 链表长度
    private Node head;  // 头节点

    // 初始化一下
    public SingleLinkedList(){
        this.size = 0;
        this.head = null;
    }

    // 链表的每一个节点
    private class Node{
        private Object data;  // 每一个节点的数据
        private Node next;   // 每一个节点指向的下一个节点
        public Node(Object data){
            this.data = data;
        }
    }

    // 在链表头添加元素
    public Object addHead(Object obj){
        // 构建新节点
        Node newNode = new Node(obj);
        // 如果没有节点
        if(size == 0){
            // 设置新创建的节点为头结点
            head = newNode;
        }else{
            // 如果节点,将 head 设置为新创建节点的 next 节点
            newNode.next = head;
            // 将新创建的节点设置为 head 头节点
            head = newNode;
        }
        size ++;
        return obj;
    }

    // 在链表头删除元素
    public Object deleteHead(){
        if(size == 0){
            return null;
        }
        // 获取头节点的值
        Object value = head.data;
        // 将头节点指向头节点的下一个节点
        head = head.next;
        // 返回删除的头节点值
        return value;
    }

    // 查找指定节点,找到返回,找不到返回 null
    public Node find(Object obj){
        if(size == 0){
            return null;
        }
        // 默认头节点为要找的节点
        Node current = head;
        // 默认为链表的 size
        int tempSize = size;
        while (tempSize > 0){
            if(head.data.equals(obj)){
                return current;
            }else{
                // 将下一个节点设置为 current 当前节点
                current = head.next;
            }
            tempSize --;
        }
        return null;
    }

    // 删除指定的元素,删除成功返回 true
    public boolean delete(Object obj){
        // 如果没有节点
        if(size == 0){
            return false;
        }
        // 设置当前节点默认为头节点
        Node current = head;
        // 设置 上一个节点 默认为 head 头节点
        Node previous = head;
        // 循环遍历所有节点直到找到一个相等的,否则放回 false
        while (current.data != obj){
            // 如果当前节点的下一个节点是 null 证明已经是最有一个节点了直接返回 false
            if(current.next == null){
                return false;
            }else{
                // 将上一个节点指当前节点
                previous = current;
                // 将当前节点指向为 当前节点的下一个节点
                current =  current.next;
            }
        }

        // 如果走到这证明已经找到节点了
        // 如果删除的是头节点,将头结点指向到 当前节点的下一个节点
        if(current == head){
            // 将头结点指向到当前节点的下一个节点
            head = current.next;
        }else{
            // 如果删除的不是头节点
            // 由于 previous 上一个节点的 next 节点本身指向的的当前节点
            // 但是现在要删除当前节点 所以将 previous 的 next 节点指向
            // current 当前节点的 next 节点就可以了
            previous.next = current.next;
        }
        size --;
        return true;
    }

    // 判断链表是否为空
    public boolean isEmpty(){
        return size == 0;
    }

    // 打印节点信息
    public void display(){
        // 有节点数据
        if(size > 0){
            // 构建 current 节点指向 头节点
            Node current = head;
            // 链表长度
            int tempSize = size;
            // 若果只有一个节点
            if(tempSize == 1){
                System.out.println("["+current.data+"]");
            }else{
                while (tempSize > 0){
                    // 头节点
                    if(current.equals(head)){
                        System.out.print("["+current.data+"->");

                        // 如果当前节点的 next 节点是 null 说明是最后一个节点
                    }else if(current.next == null){
                        // 最有一个节点
                        System.out.print(""+current.data+"]");
                    }else{
                        System.out.print(""+current.data+"->");
                    }
                    // 将当前节点指点为 next 下一个节点
                    current = current.next;
                    tempSize --;
                }
                System.out.println();
            }
        }else{
            // 没有节点数据打印空数组
            System.out.println("[]");
        }
    }

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addHead("A");
        singleLinkedList.addHead("B");
        singleLinkedList.addHead("C");
        singleLinkedList.addHead("D");
        singleLinkedList.addHead("E");

        // 打印链表信息
        singleLinkedList.display();

        // 删除指定节点
        System.out.println("删除节点:"+singleLinkedList.delete("E"));

        // 打印链表信息
        singleLinkedList.display();
    }
}

结果

[E->D->C->B->A]
删除节点:true
[D->C->B->A]
举报

相关推荐

0 条评论