一、栈

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. vue

    vue.js 插件 setting--> plugins 搜索vue,下载安装如果想要高亮显示*.vue文件,可以在File Types 选项里找到HTML,然后在下方手动添加*.vue,这样就 ...

  2. 8-06循环结构WHILE

    WHILE 循环语句可以根据某些条件重复执行一条SQL语句或一个语句块. 语句: WHILE(条件) BEGIN 语句或语句块 END 程序调试: ALT+F5启动调试 F9切换断点  F10遂过程, ...

  3. post NSURLConnection请求网络数据

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  4. xcode下载

    http://adcdownload.apple.com/Developer_Tools/Xcode_7.3.1/Xcode_7.3.1.dmg http://adcdownload.apple.co ...

  5. Fzu2124 - 吃豆人 BFS

    Description 吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住. 这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可 ...

  6. 第八章 springboot + mybatis + 多数据源

    http://www.cnblogs.com/java-zhao/p/5413845.html

  7. Browsersync + Gulp.js

    1.安装 Browsersync 和 依赖包 Gulp npm install browser-sync gulp --save-dev 2.gulpfile.js var gulp = requir ...

  8. iOS宏和__attribute__

    本文目录 iOS宏的经典用法 Apple的习惯 __attribute__ iOS宏的经典用法 1.常量宏.表达式宏 #define kTabBarH (49.0f) #define kScreenH ...

  9. js获取网页屏幕可视区域高度

    document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...

  10. 第一章-第二题Unity3D游戏引擎相关--By林培文

    1) 此类软件是什么时候开始出现的, 这些软件是怎么说服你(陌生人)成为他们的用户的?  他们的目标都是盈利么?  他们的目标都是赚取用户的现金么?还是别的? 2004年,Unity3D诞生于丹麦哥本 ...