在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

java双向链表怎么实现

101次阅读
没有评论

共计 1973 个字符,预计需要花费 5 分钟才能阅读完成。

Java 中的双向链表可以通过定义一个 Node 类来实现,该类包含一个值和两个指针,分别指向前一个节点和后一个节点。具体实现如下:

public class DoublyLinkedList {private Node head; // 链表头节点 
    private Node tail; // 链表尾节点 

    // 节点类 
    private class Node {private int value;
        private Node prev;
        private Node next;

        public Node(int value) {this.value = value;
            this.prev = null;
            this.next = null;
        }
    }

    // 在链表末尾添加节点 
    public void add(int value) {Node newNode = new Node(value);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.prev = tail;
            tail.next = newNode;
            tail = newNode;
        }
    }

    // 在指定位置插入节点 
    public void insert(int index, int value) {if (index < 0 || index > size()) {throw new IndexOutOfBoundsException("Invalid index");
        }

        if (index == 0) {Node newNode = new Node(value);
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        } else if (index == size()) {add(value);
        } else {Node cur = head;
            for (int i = 0; i < index; i++) {cur = cur.next;}

            Node newNode = new Node(value);
            newNode.prev = cur.prev;
            newNode.next = cur;
            cur.prev.next = newNode;
            cur.prev = newNode;
        }
    }

    // 删除指定位置的节点 
    public void remove(int index) {if (index < 0 || index >= size()) {throw new IndexOutOfBoundsException("Invalid index");
        }

        if (index == 0) {
            head = head.next;
            head.prev = null;
        } else if (index == size() - 1) {
            tail = tail.prev;
            tail.next = null;
        } else {Node cur = head;
            for (int i = 0; i < index; i++) {cur = cur.next;}

            cur.prev.next = cur.next;
            cur.next.prev = cur.prev;
        }
    }

    // 获取指定位置的节点值 
    public int get(int index) {if (index < 0 || index >= size()) {throw new IndexOutOfBoundsException("Invalid index");
        }

        Node cur = head;
        for (int i = 0; i < index; i++) {cur = cur.next;}

        return cur.value;
    }

    // 返回链表的大小 
    public int size() {int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
}

使用示例:

public class Main {public static void main(String[] args) {DoublyLinkedList list = new DoublyLinkedList();
        
        list.add(1);
        list.add(2);
        list.add(3);
        
        System.out.println("Size: " + list.size()); // 输出:Size: 3
        
        list.insert(1, 4);
        
        System.out.println("Size: " + list.size()); // 输出:Size: 4
        System.out.println("Element at index 1: " + list.get(1)); // 输出:Element at index 1: 4
        
        list.remove(2);
        
        System.out.println("Size: " + list.size()); // 输出:Size: 3
        System.out.println("Element at index 2: " + list.get(2)); // 输出:Element at index 2: 3
    }
}

以上代码实现了一个简单的双向链表,并提供了添加、插入、删除和获取节点的功能。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计1973字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 国产三级在线精品男人的天堂 | 亚洲视频www | 国产精品久久在线观看 | 亚洲欧美成人中文在线网站 | 国产精品国产对白熟妇 | 亚洲国产精品一区二区久久hs | 成人羞羞视频在线观看免费 | 无码人妻精品一区二 | 精品无码国产一区二区三区麻豆 | 东莞毛片| 久久天堂视频 | 亚洲精品123区在线观看 | 伊人热热久久原色播放www | 久久久在线视频精品免费观看 | 狠狠热精品免费视频 | 成人区人妻精品一熟女 | av天堂久久天堂av色综合 | 丰满少妇高潮惨叫正在播放 | 青青青爽在线视频观看 | 成年免费a级毛片 | 大地资源中文第三页 | 中文字幕电影在线 | 中文字幕在线亚洲日韩6页 中文字幕在线一区 | 久久女同互慰一区二区三区 | 日韩高清成人毛片不卡 | 热99精品只有里视频最新 | 曰韩亚洲av人人夜夜澡人人爽 | 国产大秀视频一区二区三区 | 国产成人精品免费视频网页大全 | 中文幕无线码中文字蜜桃 | 国产精品亚洲精品久久精品 | 欧美大尺度aaa级毛片 | 99热影视| 久久er这里只有精品 | 国产免费一区二区三区 | 亚洲中文字幕久久精品蜜桃 | 国产精品亚洲精品久久精品 | 大桥久未无码吹潮在线观看 | 日韩av无码一区二区三区不卡 | 7878成人国产在线观看 | 亚洲欧美日韩久久精品第一区 |