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 ( ...
随机推荐
- WCF创建RESTService
这篇博客将介绍在WCF中创建REST服务相关内容.首先先看一下的项目结构: Contract,Service两个工程为类库工程,需要添加System.ServiceModel, System.Serv ...
- 【leetcode】Remove Element
题目概述: Given an array and a value, remove all instances of that value in place and return the new len ...
- HBase change split policy on an existing table
hbase(main)::> create 'test_table_region', 'username' row(s) in 1.2150 seconds hbase(main)::> ...
- CentOS两台服务器利用scp拷贝文件
yum install -y openssh-clients scp -r -P 26611 /usr/local/ssdb-20160518/ root@10.10.6.199:/usr/local ...
- Leetcode Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- CSV格式数据如何导入SqlServer?
一.使用微软数据库IDE管理软件:Microsoft SQL Server Management Studio 1.标准的CSV文件格式如下: 2.建数据表 3.在需要导入的数据库右键点击“任务”,选 ...
- 使用功能强大的插件FastReport.Net打印报表实例
我第一次使用FastReport插件做的功能是打印一个十分复杂的excel表格,有几百个字段都需要绑定数据,至少需要4个数据源,而且用到横向.竖向合并单元格. 我不是直接连接数据库,而是使用Regis ...
- mac优秀软件介绍
1.首先是office软件: Microsoft_Office_2016_Installer.pkg 然后是一个破解软件 FWMSO2016VLU2.0.dmg_.zip 两个都不可少 2.然后是如果 ...
- Android课程---视图组件总结(2)
- finally类
finally叫做最后的执行快,什么是最后的执行快?他的意思是这样的 他是写在try catch 的后面但是只能写一个,他设计这个finally的意思就是,如果try里面出错肯定会往陷阱里 面跑.没有 ...