數(shù)據(jù)結(jié)構(gòu)的javascript實(shí)現(xiàn)

一、數(shù)組

是有序的元素序列

二、棧

后進(jìn)先出的有序集合

class Stack{
    constructor(){
        this.item = []
    }
    push(element){
        this.item.push(element)
    }
    pop(){
        return this.item.pop()
    }
    peek(){
        return this.item[this.item.length-1]
    }
    isEmpty(){
        return this.item.length === 0
    }
    clear(){
        this.item = []
    }
    size(){
        return this.item.length
    }
}

三、隊(duì)列

先進(jìn)先出的有序的項(xiàng)。

class Queue{
    constructor(){
        this.item = []
    }
    enqueue(element){
        this.item.push(element)
    }
    dequeue(){
        return this.item.shift()
    }
    front(){
        return this.item[0]
    }
    isEmpty(){
        return this.item.length === 0
    }
    size(){
        return this.item.length
    }
}

四、鏈表

鏈表是非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。

1. 單向鏈表

鏈表的鏈接方向是單向的,對(duì)鏈表的訪問要通過從頭部開始,依序往下讀取

class Node{
    constructor(element){
        this.element = element
        this.next = null
    }
}
class LinkedList{
    constructor(){
        this.length = 0
        this.head = null
    }
    append(){
        let node = new Node(element),current
        if(this.head === null){
            this.head = node
        }else{
            current = this.head
            while(current.next){
                current = current.next
            }
            current.next = node
        }
        this.length++
    }
    insert(){}
    removeAt(){}
    remove(){}
    toString(){}
    indexOf(){}
    isEmpty(){}
    size(){}
}
2. 雙向鏈表

每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。

3. 循環(huán)鏈表

最后一個(gè)結(jié)點(diǎn)指向頭結(jié)點(diǎn),形成一個(gè)環(huán)。

五、集合

無序且唯一的項(xiàng)

class Set{
    constructor(){
        this.items = {}
    }
    has(value){
        return value in this.items
    }
    add(value){
        if(!this.has(value)){
            this.items[value]=[value]
            return true
        }
        else{
            return false
        }
    }
    remove(){
        if(this.has(value)){
            delete this.items[value]
            return true
        }
        else{
            return false
        }
    }
    clear(){
        this.items = {}
    }
    size(){
        return Object.keys(items).length
    }
}

六、字典

class Dictionary{
    constructor(){
        this.items = {}
    }
    has(key){
        return key in items
    }
    set(key,value){
        items[key] = value
    }
    delete(key){
        delete items[key]
    }
    get(key){
        return this.has(key) ? items[key]:undefined
    }
    clear(){
        this.items = {}
    }
}

七、散列表(哈希)

八、樹

class Node {
    constructor(key) {
      this.key = key;
      this.left = null;
      this.right = null;
    }
}
class BinarySearchTree{
    constructor(){
        this.root = null
    }
    insert(){
        let newNode = new Node(element)
        function insertNode(node,newNode){
            if(newNode.key < node.key){
                if(node.left === null){
                    node.left = newNode
                }else{
                    insertNode(node.left,newNode)
                }
            }else{
                if(node.right === null){
                    node.right = newNode
                }else{
                    insertNode(node.right,newNode)
                }
            }
        }
        if(this.root === null){
            root = newNode
        }else{
            insertNode(root,newNode)
        }
    }
}

九、圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容