JavaScript 數據結構與算法(二)

雙向鏈表

在這裏插入圖片描述

特點

  1. 可以使用一個 head 和一個 tail 分別指向頭部和尾部的節點
  2. 每個節點都有三部分組成:前一個節點指針(prev)/保存的元素(item)/後一個節點的指針(next)
  3. 雙向鏈表的第一個節點的 prev 是null
  4. 雙向鏈表的最後的節點的 next 是null

在這裏插入圖片描述

function Node (data) {
    // 輔助類
    this.data = data;
    this.prev = null;
    this.next = null;
}
function DoublyLinkedList() {
    // 雙鏈表
    this.header = null;
    this.tail = null;
    this.length = 0;
}
// 向列表尾部添加一個新的項
DoublyLinkedList.prototype.append = function(data) {
    var newNode = new Node(data);
    if (this.length === 0 ){
        this.header = newNode;
        this.tail = newNode;
    } else {
        newNode.prev = this.tail;
        this.tail.next = newNode;
        this.tail = newNode;
    }
    this.length += 1;
}

DoublyLinkedList.prototype.toString = function() {
    return this.backwardString();
}
// 向前遍歷
DoublyLinkedList.prototype.forwardString = function() {
    var current = this.tail;
    var resutlString = "[";

    while (current) {
        resutlString += current.data + " ";
        current = current.prev
    }
    return resutlString + "]";
}
DoublyLinkedList.prototype.backwardString = function() {
    var current = this.header;
    var resutlString = "[";

    while (current) {
        resutlString += current.data + " ";
        current = current.next
    }
    return resutlString + "]";
}

DoublyLinkedList.prototype.insert = function(position, data) {
    // 越界判斷
    if (position < 0 || position > this.length) {
        console.error('越界');
        return;
    }

    var newNode = new Node(data);

    if (this.length === 0 ) {
        this.header = newNode;
        this.tail = newNode;
    } else {
        if (position === 0) {
            this.header.prev = newNode;
            newNode.next = this.header;
            this.header = newNode;
        } else if(position === this.length) {
            newNode.prev = this.tail;
            this.tail.next = newNode;
            this.tail = newNode;
        } else {
            var current = this.header;
            var index = 0;
            while (index++ < position) {
                current = current.next;
            }
            current.prev.next = newNode;
            newNode.prev = current.prev;
            current.prev = newNode;
            newNode.next = current;
        }
    }
    this.length += 1;
    return data;
}

DoublyLinkedList.prototype.get = function(position) {
    // 越界判斷
    if (position < 0 || position >= this.length) {
        console.error('越界');
        return;
    }

    var current, index;
    if(this.length / 2 >= position) {
        current = this.header;
        index = 0;
        while(index ++ < position) {
            current = current.next;
        }
        return current.data;
    } else {
        current = this.tail;
        index = this.length - 1;
        while(index -- > position) {
            current = current.prev;
        }
        return current.data;
    }

}

DoublyLinkedList.prototype.indexOf = function(data) {
    var current = this.header;
    var index = 0;

    while(current) {
        if(current.data === data) {
            return index;
        }
        current = current.next;
        index += 1;
    }

    return -1;
}
DoublyLinkedList.prototype.update = function(position, newData) {
    // 越界判斷
    if (position < 0 || position >= this.length) {
        console.error('越界');
        return;
    }

    var current = this.header;
    var index = 0;

    while(index ++ < position) {
        current = current.next;
    }
    current.data = newData;
    return true;
}

DoublyLinkedList.prototype.removeAt = function(position) {
    // 越界判斷
    if (position < 0 || position >= this.length) {
        console.error('越界');
        return;
    }

    var current = this.header;
    if (this.length === 1) {
        this.header = null;
        this.tail = null;
    } else {
        if(position === 0) {
            this.header.next.prev = null;
            this.header = this.header.next;
        } else if( position === this.length - 1) {
            current = this.tail;
            this.tail.prev.next = null;
            this.tail = this.tail.prev;
        } else {
            var index = 0;

            while(index ++ < position) {
                current = current.next;
            }
            current.prev.next = current.next;
            current.next.prev = current.prev;
        }
    }
    this.length -= 1;
    return current.data;
}

DoublyLinkedList.prototype.remove = function(data) {
    var index = this.indexOf(data);
    return this.removeAt(index);
}
DoublyLinkedList.prototype.size = function() {
    return this.length;
}
DoublyLinkedList.prototype.isEmpty = function() {
    return this.length === 0;
}
DoublyLinkedList.prototype.getHeader = function() {
    return this.header.data;
}
DoublyLinkedList.prototype.getTail = function() {
    return this.tail.data;
}

集合

其實最常用的是使用哈希表來實現集合,但我還沒學到那,就先用對象來封裝拉!

  1. 無序的不能重複的
  2. 可以看成特殊數組
  3. 不能通過下標值進行訪問,不同的重複意味着相同的對象在集合中只會存在一份

其實 ES6 中就包含了Set類,所以我們可以不封裝,直接使用它.

function Set() {
    this.items = {};
}
Set.prototype.add = function (value) {
    if (this.has(value)) {
        return false;
    }
    this.items[value] = value;
    return true;
}
Set.prototype.has = function(value) {
    return this.items.hasOwnProperty(value);
}
Set.prototype.remove = function (value) {
    if(!this.has(value)) {
        // 判斷是否有該屬性
        return false;
    }

    delete this.items[value];
    return true;
}
Set.prototype.clear = function() {
    this.items = {}
}
Set.prototype.size = function() {
    return Object.keys(this.items).length;
}
Set.prototype.values = function() {
    return Object.keys(this.items)
}
// 並集
Set.prototype.union = function(otherSet) {
    // this 表示當前集合 A
    // otherSet 表示另一個集合 B

    var unionSet = new Set();
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        unionSet.add(values[i]);
    }
    values = otherSet.values();
    for(var i = 0; i < values.length; i++) {
        unionSet.add(values[i]);
    }
    return unionSet;
}
// 交集
Set.prototype.intersection = function(otherSet) {
    var intersection = new Set();

    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        var item = values[i];
        if(otherSet.has(item)) {
            intersection.add(item);
        }
    } 

    return intersection;
}
// 差集
Set.prototype.difference = function(otherSet) {

    var differenceSet = new Set();

    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        var item = values[i];
        if(!otherSet.has(item)) {
            differenceSet.add(item);
        }
    }
    return differenceSet;
}
// 判斷子集:A 是不是 B 的子集
Set.prototype.subset = function(otherSet) {
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        var item = values[i];
        if(!otherSet.has(item)) {
            return false;
        }
    }
    return true;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章