用JavaScript擼一個靜態鏈表

最近重新開始翻起《大話數據結構》,看到了靜態鏈表部分裏面講C語言是利用數組模擬,覺得十分有趣。但是在JavaScript中,也可以用類似的方式去實現,定義一個數據域和一個結點域,然後實現鏈表的基礎操作。弱類型語言沒有指針,所以需要自己區實現。算法的樂趣就在於解決一些思路上的問題,直擊問題的本質。
首先可以定義Node類,如下所示:

class Node {
    constructor(value) {
        this.data = value;
        this.next = null;
    }
}

然後實現StaticLinkedList類,先定義簡單的append和display方法:

class StaticLinkedList {
    constructor() {
        this.head = null;
        this.length = 0;
    }

    append(value) {
        const newNode = new Node(value);
        this.length++;
        if (this.head === null) {
            this.head = newNode;
            return;
        }
        let current = this.head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }

    display() {
        console.log('the static linked list is:\r\n');
        let current = this.head;
        if (current === null) {
            console.log('empty!');
            return;
        }
        while (current !== null) {
            console.log(JSON.stringify(current));
            console.log(`its value is ${current.data}\r\n`);
            current = current.next;
        }
    }
}

其中append方法是在鏈表尾部添加新的Node對象,display方法可以打印出Node對象和它的數據。使用這個靜態鏈表類也很簡單,比如添加4個結點到這個鏈表裏面:

const staticLinkedList = new StaticLinkedList();
staticLinkedList.append(3);
staticLinkedList.append(7);
staticLinkedList.append(16);
staticLinkedList.append(24);

我們還應該提供更加靈活添加結點的方法,比如我想在第三個結點位置插入一個新的結點,數值爲11,那麼現有的append方法就不適用了,需要定義一個新的插入結點的方法,代碼如下:

    /**
     * Method to insert an new element at the specific location
     *
     * @param {*} elementValue the value of the element that to be inserted
     * @param {*} index the position of the element, from 1 to maximum of the list
     * @returns true/false
     */
    insertAt(elementValue, index) {
        if (index < 1 || index > this.length + 1) {
            console.log('index is out of the range!');
            return false;
        }

        const newNode = new Node(elementValue);
        let startPos = 1;
        let current = this.head;
        while (startPos < index - 1) {
            current = current.next;
            startPos++;
        }
        newNode.next = current.next;
        current.next = newNode;
        this.length++;
        return true;
    }

這段代碼需要理解的是新結點如何添加到鏈表的那兩行代碼,首先是newNode.next = current.next,這行代碼是把新結點的next指向了原來插入前位置的結點的下一個結點。然後current.next = nextNode,把新結點替換掉原來該位置的結點。

爲了更好地理解,我畫了一張示意圖:

要注意的是step1和step2的順序不能顛倒,否則會導致代碼運行錯誤。

然後我們還需要定義一個移除指定位置結點的方法,如下所示:

removeAt(index) {
        if (index < 1 || index > this.length + 1) {
            console.log('index is out of the range!');
            return;
        }
        let current = this.head;
        let startPos = 1;
        let previous = null;
        while (startPos < index) {
            previous = current;
            current = current.next;
            startPos++;
        }

        previous.next = current.next;
        this.length--;
    }

我對previous.next = current.next也畫了一張示意圖,刪除原來結點,需要把它前面一個結點的next指向該結點的next。

總結:
靜態鏈表的添加和移除略有不同,需要利用Node中的next進行模擬指針操作,讓新的結點加入到鏈表,讓需要被刪除的結點直接從上一個結點的next指向到原有結點的next。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章