前端笔记

/**
 * Created by liw on 2017/7/3.
 */
var alg = {
    sortAlg: {
        testArr: [13, 9, 12, 78, 9, 51, 23, 97, 58, 4, 8, 97, 54, 7, 99, 5, 32, 75, 233, 86, 44, 3],
        //平均来说插入排序算法复杂度为 O(n^{2}。因而,插入排序不适合对于数据量比较大的排序应用
        insertionSort: function (arr) {
            var temp, j;
            for (var i = 1; i < arr.length; i++) {
                temp = arr[i];
                j = i - 1;
                for (; j >= 0 && arr[j] > temp; j--) {
                    arr[j + 1] = arr[j];//前一个的值给后一个。
                }
                arr[j + 1] = temp;//当找的数 不大于 temp 或者到j=-1时。把temp插入正确的位置。
            }
        },
        quickSort: function (arr) {
            var len = arr.length;
            if (len <= 1) {
                return arr.slice(0);
            }
            var less = [], pivot = [arr[0]], greater = [];
            for (var i = 1; i < len; i++) {//注意循环不应再插入pivot。
                if (arr[i] < pivot[0]) {
                    less[less.length] = arr[i];//数组对象头里有_length记录数组长度属性,所以我认为这样写比push优。
                } else {
                    greater[greater.length] = arr[i];
                }
            }
            return this.quickSort(less).concat(pivot.concat(this.quickSort(greater)));
        },
        mergeSort: function (arr) {
            var merge = function (left, right) {
                var final = [];
                while (left.length && right.length) {
                    final.push(left[0] <= right[0] ? left.shift() : right.shift());
                }
                return final.concat(left.concat(right));
            }
            var len = arr.length;
            if (len < 2) return arr;
            var mid = len / 2;
            return merge(this.mergeSort(arr.slice(0, parseInt(mid))), this.mergeSort(arr.slice(parseInt(mid))));
        }
    },
    mathAlg:{
        pow: function (n, m) {
            if(n==1) return 1;
            else if(m == 1) return n;
            return n*this.pow(n,m-1);
        },
        factorial: function (n) {
            if(n==1 || n==0) return 1;
            return n*this.factorial(n-1);
        }
    },
    objAlg: {
        copyJson: function (json) {
            var newJson = {};
            for (var i in json) {
                if (typeof json[i] == 'object') {
                    newJson[i] = this.copyJson(json[i]);
                } else {
                    newJson[i] = json[i];
                }
            }
            return newJson;
        }
    }
}
/**
 * 数组和链表的区别
 * 数组中,每个元素连续存储在内存中,可以通过下标迅速访问。如果增加或删除数组元素,需要移动其后元素,腾位或补位。
 * 数组方便查询,不方便增删。C++中不允许动态定义数组的大小。牵一发而动全身
 * 链表相反,链表中元素在内存中不是顺序存储的,是通过存在元素中的指针联系到一起。
 * 每一个元素都是由元素本身数据和指向下一个元素的指针构成。
 * (自行车的链条,环环相扣),但添加或是移除某一个环节只需要对症下药,对相关环节进行操作就OK。
 * (上个元素有个指针指向下个元素,以此类推直到最后一个元素),若要访问某一个元素,需要从第一个元素开始
 * 一直找到需要的元素位置。
 * 链表擅增删元素,只需修改元素指针。
 */

/**
 * 事件冒泡可以形象地比喻为把一颗石头投入水中,泡泡会一直从水底冒出水面。也就是说,事件会从最内层的元素开始发生,一直向上传播,直到document对象。
 * 事件捕获(event capturing)。与事件冒泡相反,事件会从最外层开始发生,直到最具体的元素。
 * element.addEventListener(event,function,useCapture)
 * 第一个参数需要绑定的事件
 * 第二个触发事件执行
 * 第三个默认是false,表示事件冒泡阶段调用。true表示捕获阶段调用
 */
//方法一:事件冒泡--begin
// var badFun = (function () {
//     var colorList = document.getElementById("color-list");
//     var colors = colorList.getElementsByTagName("li");
//     for(var i = 0; i < colors.length; i++){
//         colors[i].addEventListener("click",showColor,false);
//     }
//     function showColor(e){
//         var tar = e.target;
//         console.log(tar.innerHTML);
//     }
// })();
//方法二:快  ==> 事件捕获
// var goodFun = function () {
//     var coloList = document.getElementById("color-list");
//     coloList.addEventListener("click",showColor,true);
//     function showColor(e) {
//         var tar = e.target;
//         if(tar.nodeName.toUpperCase() == "LI"){
//             console.log(tar.innerHTML);
//         }
//     }
// }
// -- end
/**
 * 二叉树遍历:(深度遍历 和 广度遍历)
 * 前序、中序、后序,(查询根节点先中后)
 */

//实现链表
function LinkedList(){
    let Node = function (element) {
        this.element = element;
        this.next = null;
    }
    let length = 0;
    let head = null;
    this.append = function (element) {
        let node = new Node(element),
            current;
        if(head === null){
            head = node;
        }else{
            current = head;
            while(current.next){//找到最后一个节点
                current = current.next;//current.next的next是null
            }
            current.next = node;
        }
        length++;
    }
    this.insert = function (position,element) {
        if(position >= 0 && position <= length) {
            let node = new Node(element),
                current = head,
                previous,
                index = 0;
            if (position === 0) {
                node.next = current;
                head = node;
            } else {
                while (index++ < position) {//最后index等于position
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;
            }
            length++;
            return true;
        }else{
            return false;
        }
    }
    this.removeAt = function(position){
        if(position>=0 && position<=length){
            let index = 0,
                previous,
                current = head;
            if(position === 0){
                head = current.next;
            }else {
                while (index++ < position) {
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
            }
            length--;
            return current.element;
        }else{
            return null;
        }
    }
    this.getNode = function (position) {
        if(position>=0 && position<=length){
            let index = 0,
                current = head;
            if(position !== 0){
                while(index++ < position){
                    current = current.next;
                }
            }
            return current.element;
        }else{
            return null;
        }
    }
    this.indexOf = function(element){
        let current = head,
            index = 0;
        while (current){
            if(element === current.element){
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    }
    this.remove = function (element) {//也可以indexOf然后removeAt
        let current = head,
            previous,
            index = 0;
        while(current){
            if(element === current.element){
                if(index === 0){
                    previous = head;
                    head = current.next;
                }else{
                    previous.next = current.next;
                    length--;
                }
                return current.element;
            }
            index++;
            previous = current;
            current = current.next;
        }
    }
    this.isEmpty = function (){
        return length === 0;
    }
    this.size = function () {
        return length;
    }
    this.getHead = function () {
        return head;
    }
    this.toString = function () {
        let current = head,
            string = '';
        while (current){
            string += current.element + (current.next ? ',':'');
            current = current.next;
        }
        return string;
    }
    this.print = function () {
        console.log(this.toString());
    }
}
// 实现Map (ES6已实现)
// map的value值可以重复。 set存储已排序的无重复的元素
function Map() {
    let array = [];
    let length = 0;
    let Item = function (key,value) {
        this.key = key;
        this.value = value;
    }
    this.refresh = function(item){
        let _this = this;
        for(let i = 0; i < array.length; i++){
        // array.forEach(val=>{//forEach是异步的,这里要小心
            console.log(array[i]);
            if(array[i].key == item.key){
                array[i].value = item.value;
                return array;
            }
        }
        array.push(item);
        return array;
    }
    this.set = function (key, value) {
        let item = new Item(key,value);
        this.refresh(item);
    }
    this.put = this.set;
    this.get = function (key) {
        array.forEach((val,index)=>{
            if(val.key == key){
                return array[index];
            }
        });
        return false;
    }
    this.putAll = function (map) {
        if(this.isMap){
            map.getProto().forEach(item=>{
                this.refresh(item);
            })
        }else{
            alert("传入非map类型");
        }
        return array;
    }
    this.each = function (callback) {
        array.forEach(item=>{
            callback(item.key,item.value);
        })
    }
    this.isMap = function (map) {
        return map instanceof this;
    }
    this.size = function () {
        return array.length;
    }
    this.getProto = function () {
        return array;
    }
    this.toString = function () {
        let _toString = '{';
        array.forEach(item=>{
            _toString += item.key + '=' + item.value + (',');
        });
        _toString = _toString.substring(0,_toString.length-1);
        _toString += '}';
        return _toString;
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值