久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java雙向鏈表怎么實(shí)現(xiàn)

共計(jì) 1973 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。

Java 中的雙向鏈表可以通過(guò)定義一個(gè) Node 類來(lái)實(shí)現(xiàn),該類包含一個(gè)值和兩個(gè)指針,分別指向前一個(gè)節(jié)點(diǎn)和后一個(gè)節(jié)點(diǎn)。具體實(shí)現(xiàn)如下:

public class DoublyLinkedList {private Node head; // 鏈表頭節(jié)點(diǎn) 
    private Node tail; // 鏈表尾節(jié)點(diǎn) 

    // 節(jié)點(diǎn)類 
    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;
        }
    }

    // 在鏈表末尾添加節(jié)點(diǎn) 
    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;
        }
    }

    // 在指定位置插入節(jié)點(diǎn) 
    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;
        }
    }

    // 刪除指定位置的節(jié)點(diǎn) 
    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;
        }
    }

    // 獲取指定位置的節(jié)點(diǎn)值 
    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
    }
}

以上代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的雙向鏈表,并提供了添加、插入、刪除和獲取節(jié)點(diǎn)的功能。

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-16發(fā)表,共計(jì)1973字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 连城县| 包头市| 通榆县| 庄浪县| 高密市| 平乐县| 余庆县| 册亨县| 林甸县| 澄城县| 南部县| 瑞金市| 宁明县| 保定市| 波密县| 视频| 乐安县| 尉氏县| 富民县| 云南省| 磴口县| 杭锦旗| 宣汉县| 威宁| 郓城县| 兰州市| 民乐县| 若羌县| 莱阳市| 平阳县| 靖江市| 清流县| 广饶县| 始兴县| 廊坊市| 红原县| 石渠县| 扎兰屯市| 苍南县| 北安市| 黔西县|