演算法和資料結構在JS中的運用(三)

xiaobangsky發表於2020-12-01

佇列(Queue)

  1. 特點:先進先出(FIFO)
  2. 舉例
/*
*適用於餐廳取號
**/
const queue = [];
let num = 0;

push= () => {
    num += 1;
    queue.push.call(queue,num);
}
pop = () => {
    const n = queue.shift.call(queue); // 等價於 queue.shift()
    // if n === undefined 沒處理
    if(n === undefined){
        return;
    }
};
棧(Stack)
  1. 特點:先進後出(LIFO)
  2. 舉例
stack = () => {
    let items = [];

    this.push = (element) => {
        items.push(element);
    }

    this.pop = () => {
        return items.pop();
    }

    this.peek = () => {
        return items[items - 1];
    }

    this.empty = () => {
        return items.length === 0;
    }

    this.size = () => {
        return items.length;
    }

    this.clear = () => {
        items = [];
    }

    this.print = () => {
        console.log(items.toString());
    }
}
連結串列(linked-list)

在這裡插入圖片描述
在這裡插入圖片描述

const createList = value => {
    return createNode(value);
}

const appendList = (list,value) => {
    const node = createNode(value);
    let x = list;
    while(x.next){
        x = x.next;
    }
    x.next = node;
    return node;
}

const removeFromList = (list,node) => {
    let x = list;
    let p = node;
    while(x !== node && x !== null){
        p = x;
        x = x.next;
    }
    if( x === null ) return false;
    else if(x === p){
        p = x.next;
        return p;
    }else{
        p.next = x.next;
        return list;
    }
}

const createNode = value => {
    return {
        data: value,
        next: null
    };
};

const travelList = (list,fn) => {
    let x = list;
    while( x !== null){
        fn(x);
        x = x.next;
    }
}

const list = createList(10);
const node2 = appendList(list,20);
const node3 = appendList(list,30);
const node4 = appendList(list,40);
travelList(list,node => {
    console.log(node.data);
});
樹(tree)
  1. 舉例
/*
*適用於中國的省市區
*公司的組織架構
*網頁中的節點
**/
const createTree = value => {
    return {
        data: value,
        children: null,
        parent: null
    }
}

const addChild = (node,value) => {
    const newNode = {
        data: value,
        children: null,
        parent: node
    };
    node.children = node.children || [];
    node.children.push(newNode);
    return newNode;
}

const travel = (tree,fn) => {
    fn(tree);
    if(!tree.children){
        return;
    }else if(tree.children){
        for(let i = 0; i < tree.children.length; i++){
            const result = find(tree.children[i], node);
            if(reuslt){
                return result;
            }
        }
        return undefined;
    }else{
        return undefined;
    }
};

const removeNode = (tree,node) => {
    const siblings = node.parent.children;
    let index = 0;
    for(let i = 1; i < siblings.length ; i++){
        if (siblings[i] === node) {
            index = i;
        }
    }
    siblings.splice(index, 1);
}

const tree = createTree(10);
const node2 = addChild(tree, 20);
const node3 = addChild(tree, 30);
addChild(tree, 40);
const node5 = addChild(tree, 50);
addChild(node2, 201);
addChild(node2, 202);
addChild(node2, 203);
addChild(node2, 204);
console.log(tree);

const fn = node => {
  console.log(node.data);
};

removeNode(tree, node5);
console.log(tree);

相關文章