javascript数据结构和算法
一、栈
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数据结构和算法的更多相关文章
- javascript数据结构与算法--高级排序算法
javascript数据结构与算法--高级排序算法 高级排序算法是处理大型数据集的最高效排序算法,它是处理的数据集可以达到上百万个元素,而不仅仅是几百个或者几千个.现在我们来学习下2种高级排序算法-- ...
- javascript数据结构与算法-- 二叉树
javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...
- javascript数据结构与算法--散列
一:javascript数据结构与算法--散列 一:什么是哈希表? 哈希表也叫散列表,是根据关键码值(key,value)而直接进行访问的数据结构,它是通过键码值映射到表中一个位置来访问记录的,散列 ...
- javascript数据结构与算法---队列
javascript数据结构与算法---队列 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素.队列用于存储按顺序排列的数据,先进先出,这点和栈不一样(后入先出).在栈中,最后入栈的元素 ...
- javascript数据结构与算法---栈
javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...
- javascript数据结构与算法---列表
javascript数据结构与算法---列表 前言:在日常生活中,人们经常要使用列表,比如我们有时候要去购物时,为了购物时东西要买全,我们可以在去之前,列下要买的东西,这就要用的列表了,或者我们小时候 ...
- 前端开发周报: CSS 布局方式方式与JavaScript数据结构和算法
前端开发周报:CSS 布局方式与JavaScript动画库 1.常见 CSS 布局方式详见: 一些常见的 CSS 布局方式梳理,涉及 Flex 布局.Grid 布局.圣杯布局.双飞翼布局等.http: ...
- javascript数据结构与算法---二叉树(删除节点)
javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...
- javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)
javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...
- javascript数据结构与算法---检索算法(二分查找法、计算重复次数)
javascript数据结构与算法---检索算法(二分查找法.计算重复次数) /*只需要查找元素是否存在数组,可以先将数组排序,再使用二分查找法*/ function qSort(arr){ if ( ...
随机推荐
- Matlab的部分文件操作
Author:Maddock Date:2015-01-20 判断文件是否存在 infilename = [str,'\lena.jpg']; sgc_exist = exist(infilename ...
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...
- 300ms延时
具体参考:http://www.jianshu.com/p/6e2b68a93c88 一,简单粗暴型:禁用缩放 <meta name="viewport" content=& ...
- java 猜数字游戏
作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...
- C# 的EF框架怎么连接Oracle数据库
安装odp.net ODP.NET你不需要安装Oracle,不需要配置oracle.key文件,不需要配置TnsNames.Ora文件 不需要配置环境变量:完全的傻瓜式的在没有安装oracle数据库或 ...
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- Eclipse,到了说再见的时候了——Android Studio最全解析
转自:http://blog.jobbole.com/77635/ 去年的Google大会上,Google带给我们一个小玩具——Android Studio,说它是玩具,是因为它确实比较菜,界面过时, ...
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...
- 团队作业week9 scenario testing
1.How do you expect different personas to use your software? What’s their need and their goals, how ...
- PHP基础知识之数组
数组的定义: array( key => value , ... ) // 键(key)可以是一个整数或字符串,键可以省略,默认从0开始索引 // 值(value)可以是任意类型的值或者简写的方 ...