一、栈

javascript实现栈的数据结构(借助javascript数组原生的方法即可)

//使用javascript来实现栈的数据结构
var Stack={
    //不需要外界传参进行初始化,完全可以定义为一个属性
    items:[],
    push:function(item){
        this.items.push(item);
    },
    pop:function(){
        return this.items.pop();
    },
    peek:function(){
        ];
    },
    isEmpty:function(){
        ;
    },
    size:function(){
        return this.items.length;
    },
    clear:function(){
        this.items=[];
    },
    print:function(){
        console.log(this.items.toString());
    }
};

javascript栈的应用案例(十进制和其他进制的转换)

//基于栈的数据结构实现进制转换,比如将十进制转换为2进制
function baseConvert(number,base){
    var result=Object.create(Stack),
        rem,
        baseString='',
        digits='0123456789ABCDEF',
        decNumber=number;
    ){
        rem=Math.floor(decNumber%base);
        result.push(rem);
        decNumber=Math.floor(decNumber/base);
    }

    while(!result.isEmpty()){
        baseString+=digits[result.pop()];
    }
    return baseString;
}

console.log(baseConvert(,));//输出11000011111111001
console.log(baseConvert(,));//输出303771
console.log(baseConvert(,));//输出187F9

进制转换

二、队列

javascript实现队列的数据结构

//实现队列
var Queue={
    item:[],
    enqueue:function(item){
        this.items.push(item);
    },
    dequeue:function(){
        return this.items.shift();
    },
    isEmpty:function(){
        ;
    },
    front:function(){
        ];
    },
    clear:function(){
        this.items=[];
    },
    size:function(){
        return this.items.length;
    }
};

队列

三、链表

单项链表

感觉在javascript中实现单项链表意义不大,因为单项链表的优势在于插入和删除,可是javascript的splice方法就可以实现了呀

//单链表的实现
//感觉在javascript中实现单链表没有什么用
//因为单链表的有事在于插入,删除元素的时间复杂度为O(1),可以javascript中利用数组,调用数组的splice方法,就可以实现这一目的
var Node={
    init:function(element){
        this.element=element;
        this.next=null;
    },
};
var LinkedList={
    length:,
    head:null,//指向队列的头结点
    //尾部插入元素
    append:function(element){
        var node=Object.create(Node),
            current;
        node.init(element);

        if(!this.head){
            this.head=node;
        }else{
            current=this.head;
            while(current.next){
                current=current.next;
            }
            current.next=node;
        }
        this.length++;
        return true;
    },
    //在任意一个位置插入一个元素
    insert:function(position,element){
        ||position>=this.length){
            return false;
        }
        var node=Object.create(Node),
            current=this.head,
            previous=this.head,
            index=;
        node.init(element);
        ){
            node.next=this.head;
            this.head=node;
        }else{
            while(index<position){
                index++;
                previous=current;
                current=current.next;
            }
            node.next=current;
            previous.next=node;
        }
        this.length++;
        return true;
    },
    //从链表中移除元素
    removeAt:function(position){
        var current=this.head,
            previous,
            index=;
        ||position>=this.length){
            return false;
        }
        ){
            this.head=this.head.next;
        }else{
            while(index<position){
                index++;
                previous=current;
                current=current.next;
            }
            previous.next=current.next;
        }
        this.length--;
        return true;
    },

    //接收一个元素的值,如果在列表中找到它,就返回元素位置,否则返回-1
    indexOf:function(element){
        var current=this.head,
            index=;
        while(current){
            if(current.element==element){
                return index;
            }else{
                index++;
                current=current.next;
            }
        }
        ;
    },
    isEmpty:function(){
        ;
    },
    size:function(){
        return this.length;
    },
    //将LinkedList对象转换为一个字符串
    toString:function(){
        var current=this.head,
            baseStr='';
        while(current){
            baseStr+=current.element+',';
            current=current.next;
        }
        return baseStr;
    },
    print:function(){
        console.log(this.toString());
    }
};

单项链表实现

此外还有双向链表,双向循环链表。这里不多做讨论、

四、集合

//实现集合这种数据结构
//基本思路是基于javascript对象结构的键值来实现,当然也可以基于数组
var Set={
    init:function(){
        this.items=Object.create(null);
    },
    has:function(item){
        return item in this.items;
    },
    add:function(item){
        if(!this.has(item)){
            this.items[item]=item;
            return true;
        }
        return false;
    },
    remove:function(item){
        if(this.has(item)){
            delete this.items[item];
            return true;
        }
        return false;
    },
    clear:function(){
        this.items={};
    },
    size:function(){
        return Object.keys(this.items).length;
    },
    values:function(){
        return Object.keys(this.items);
    },
    //求并集
    union:function(otherSet){
        var result=Object.create(Set),
            key;
        result.init();
        for(key in this.items){
            result.add(key);
        }
        for(key in otherSet.items){
            result.add(key);
        }
        return result;
    },
    //求交集
    intersection:function(otherSet){
        var result=Object.create(Set),key;
        result.init();
        for(key in this.items){
            if(otherSet.has(key)){
                result.add(key);
            }
        }
        return result;
    },

};

集合

javascript数据结构和算法的更多相关文章

  1. javascript数据结构与算法--高级排序算法

    javascript数据结构与算法--高级排序算法 高级排序算法是处理大型数据集的最高效排序算法,它是处理的数据集可以达到上百万个元素,而不仅仅是几百个或者几千个.现在我们来学习下2种高级排序算法-- ...

  2. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  3. javascript数据结构与算法--散列

    一:javascript数据结构与算法--散列  一:什么是哈希表? 哈希表也叫散列表,是根据关键码值(key,value)而直接进行访问的数据结构,它是通过键码值映射到表中一个位置来访问记录的,散列 ...

  4. javascript数据结构与算法---队列

    javascript数据结构与算法---队列 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据,先进先出,这点和栈不一样(后入先出).在栈中,最后入栈的元素 ...

  5. javascript数据结构与算法---栈

    javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...

  6. javascript数据结构与算法---列表

    javascript数据结构与算法---列表 前言:在日常生活中,人们经常要使用列表,比如我们有时候要去购物时,为了购物时东西要买全,我们可以在去之前,列下要买的东西,这就要用的列表了,或者我们小时候 ...

  7. 前端开发周报: CSS 布局方式方式与JavaScript数据结构和算法

    前端开发周报:CSS 布局方式与JavaScript动画库 1.常见 CSS 布局方式详见: 一些常见的 CSS 布局方式梳理,涉及 Flex 布局.Grid 布局.圣杯布局.双飞翼布局等.http: ...

  8. javascript数据结构与算法---二叉树(删除节点)

    javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...

  9. javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)

    javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...

  10. javascript数据结构与算法---检索算法(二分查找法、计算重复次数)

    javascript数据结构与算法---检索算法(二分查找法.计算重复次数) /*只需要查找元素是否存在数组,可以先将数组排序,再使用二分查找法*/ function qSort(arr){ if ( ...

随机推荐

  1. supervisor拉起daemon进程(falcon-agent)测试

    falcon-agent started..., pid= falcon-agent now is running already, pid= falcon-agent now is running ...

  2. 前台处理json字符串的几种方法(转)

    原文地址http://www.css88.com/archives/3919 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":&quo ...

  3. 分享Kali Linux 2016.2第50周镜像文件

    分享Kali Linux 2016.2第50周镜像文件Kali Linux官方于12月11日发布Kali Linux 2016.2的第50周镜像.这次保持以往规律,仍然是11个镜像文件.默认的Gnom ...

  4. ACM:UESTC - 649 括号配对问题 - stack

      UESTC - 649  括号配对问题 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu ...

  5. div垂直居中的几种方法

    CSS教程:div垂直居中的N种方法[转](原文地址:http://www.cnblogs.com/chuncn/archive/2008/10/09/1307321.html) 在说到这个问题的时候 ...

  6. jQuery常用插件

    jQuery UI插件简介: jQuery UI是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互.动画.特效和可更换主题的可视控件.我们可以直接用它来构建具 ...

  7. crontab中执行任务定位到秒级

    每秒执行一次: * * * * * /bin/sleep 1 ; echo "1"

  8. uva116

    这题是一道dp的水题,类似于数塔,从右向左倒着加上去,找到最小值,然后在从左到右输出路径. #include"iostream" #include"stdio.h&quo ...

  9. 浏览器对象模型BOM小结

    概念 BOM (Browser Object Model) 浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象是window B ...

  10. log4j mongoDB配置

    log4j.rootCategory=INFO, stdout log4j.appender.stdout=org.springframework.data.document.mongodb.log4 ...