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 ( ...
随机推荐
- Hibernate的事务处理
事务:事务就是逻辑上的一组操作,要么全都成功,要么全都失败!!! 事务特性 原子性:事务一组操作不可分割. 一致性:事务的执行前后,数据完整性要保持一致. 隔离性:一个事务在执行的过程中不应该受到其他 ...
- css3 tween
/* * Tween.js * t: current time(当前时间) * b: beginning value(初始值) * c: change in value(变化量) * d: durat ...
- C#中的Lambda表达式的演化过程
原文:http://www.cnblogs.com/zhaopei/p/5767631.html
- appium依赖pom文件注解
<!-- appium部分 依赖--> <dependency> <groupId>io.appium</groupId> <artifactId ...
- 自己写了一个无缝滚动的插件(jQuery)
效果图: html代码: 1 <h1>无缝滚动,向右滚动</h1> 2 <ul id="guoul1"> 3 <li><img ...
- 简单说说call 与apply
Function.call() 将函数作为对象的方法调用,例如:function.call(thisobj,args,........); thisobj 调用function的对象.在函数主体中, ...
- xp系统重绘边框线不显示(首次加载没有触发paint事件)
同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...
- php+ajax 登录注册页面
主要是登录注册功能,前端后台验证没有什么,这个大家可以自己加上去,比如过滤啊,正则啊等 还是先放图吧 这是登录及注册界面 点击注册切换到注册界面,点击登录切换到登录界面 <!DOCTYPE h ...
- Javascript初学篇章_1(概念/数据类型)
Javascript是一门脚本语言,主要由浏览器来执行.它可以说是页面的灵魂,让页面活过来.与之前学的HTML5+CSS样式的不同之处就在于,JS能让静态网页成为一个动态网页,实现与用户的互动. Ja ...
- JQuery实现方法,简单示例
function $() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var eleme ...