前端需要掌握的数据结构与算法

主要有:树、队列、栈、链表、递归、动态规划等。

树

为什么需要掌握树?
在网页中我们常看到的级联菜单、省市区联动选择等都是需要用到树的知识 ,对于前端开发来说是必不可少的。
递归

递归的思想算是js中比较重要的思想了。
一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。

队列和栈

参考:栈和队列
1)队列:一种特殊的线性表,特殊之处在于它只允许在表的队头进行删除操作,而在表的队尾进行插入操作,即先进先出(FIFO)。
js中实现队列的方法是:
结合使用数组的push和shift方法实现正向队列;使用unshift和pop方法实现反向队列。

function Queue() {
    var items = [];
    this.enqueue = function(element){
        items.push(element);
    };
    this.dequeue = function(){
        return items.shift();
    };
    this.front = function(){
        return items[0];
    };
    this.isEmpty = function(){
        return items.length == 0;
    };
    this.clear = function(){
        items = [];
    };
    this.size = function(){
        return items.length;
    };
    this.print = function(){
        console.log(items.toString());
    };
}

2)栈:一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算,即后进先出(LIFO)。
js实现堆栈:
结合使用数组的push和pop方法;使用unshift和shift方法,不过栈顶在数组开头。

function Stack() {
    var items = [];
    this.push = function(element){
        items.push(element);
    };
    this.pop = function(){
        return items.pop();
    };
    this.peek = function(){
        return items[items.length-1];
    };
    this.isEmpty = function(){
        return items.length == 0;
    };
    this.size = function(){
        return items.length;
    };
    this.clear = function(){
        items = [];
    };
    this.print = function(){
        console.log(items.toString());
    };
    this.toString = function(){
        return items.toString();
    };
}
链表

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表是一种常见的重要的数据结构。是动态地进行存储分配的一种结构,可以根据需要开辟内存单元。链表有一个“头指针”变量,以head表示,它存放一个地址,该地址指向一个元素。链表中每一个元素称为“结点”,每个结点都应包括两个部分:一为用户需要用的实际数据,二为下一个结点的地址。因此,head指向第一个元素:第一个元素又指向第二个元素;……,直到最后一个元素,该元素不再指向其它元素,它称为“表尾”,它的地址部分放一个“NULL”(表示“空地址”),链表到此结束。
链表的各类操作包括:单向链表的创建、删除、 插入(无序、有序)、输出、 排序(选择、插入、冒泡)、反序等等。
js的原型、原型链和链表的概念有相似的地方,掌握链表的思想有助于理解js的原型的相关知识。

var Node = function(newData){  
        this.next = null;  
        this.data = null;  
        this.Init = function(){  
            this.data = newData;  
        };  
        this.Init();  
    }  
    //definition of List class   
    var List = function(){  
        this.head = null;  
        this.size = 0;  
        this.Init = function(){  
            this.head = null;  
            this.size = 0;  
        }  
        this.Init();  
          
        this.Insert = function(newData){  
            this.size += 1;  
            var newNode = new Node(newData);  
            if(this.head == null){  
                this.head = newNode;  
                return;  
            }  
            var tempNode = this.head;  
            while(tempNode.next != null)  
                tempNode = tempNode.next;  
            tempNode.next = newNode;  
        };  
          
        this.GetData = function(pos){  
            if(pos >= this.size || pos < 0)  
                return null;    
            else{  
                tempNode = this.head;  
                for(i = 0;i < pos;i++)    
                    tempNode = tempNode.next;    
                return tempNode.data;    
             }  
        };  
          
        //remove the element at pos  
        this.Remove = function(pos){  
            if(pos >= this.size || pos < 0)  
                return null;      
            this.size -= 1;  
            tempNode = this.head;  
            if(pos == 0){  
                this.head = this.head.next;  
                return this.head;  
            }  
            for(i = 0;i < pos - 1;i++){  
                tempNode = tempNode.next;  
            }  
            tempNode.next = tempNode.next.next;  
            return tempNode.next;  
                  
        }  
          
        this.Print = function(){  
            document.write("elements in list as follows: <br>");  
            tempNode = this.head;  
            while(tempNode != null){  
                document.write(tempNode.data + " ");  
                tempNode = tempNode.next;  
            }  
            document.write("<br>");  
        };  
    };  
      
    //RUN TEST:  
    var list = new List();  
    var array = new Array(1,2,3,4,5,6);  
    for(i = 0;i < array.length;i++){  
        list.Insert(array[i]);  
    }  
    list.Print();  
    document.write("now remove action: <br>");  
    list.Remove(5);  
    list.Print();  
    document.write("new size after Remove list[5]:  " + list.size); 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值