/**哈希表*/
var HashMap = function(){
this.table={};
this.count=0;//长度
/**添加对象
* @param key 键
* @param obj 对象
*/
this.put=function(key,obj){
var v=this.table[key];
if(typeof(v)=="undefined")this.count+=1;
this.table[key]=obj;
};
/**获得对象
* @param key 键
* @return obj 对象
*/
this.get=function(key){
return this.table[key];
};
this.contains=function(key){
return (key in this.table);
};
/**删除对象
* @param key 键
* @return obj 对象
*/
this.remove=function(key){
var obj=this.table[key];
if(obj != undefined){
delete this.table[key];
this.count -= 1;
}
return obj;
};
/**清空所有对象*/
this.clear=function(){
this.table={};
this.count=0;
};
/**当前大小*/
this.size=function(){
return this.count;
};
/**是否为空*/
this.isEmpty=function(){
return this.count <= 0;
};
/**获得所有值*/
this.values=function(){
var values = new Array();
for(var prop in this.table){
values.push(this.table[prop]);
}
return values;
};
/**获得所有键*/
this.keys=function(){
var keys = new Array();
for(var prop in this.table){
keys.push(prop);
}
return keys;
};
};
/**顺序链表*/
var ArrayList = function(){
this.count=0;//长度
this.initialCapacity = 10;
this.list=new Array(this.initialCapacity);
/**添加对象
* @param obj 对象
*/
this.add=function(obj){
this.list[this.count]=obj;
this.count+=1;
};
/**按索引设置对象
* @param obj 对象
*/
this.set=function(index,obj){
if(this.list.length=0&&index=0){
var start=index;var end=index+length;
var counts=0;var lists=[];
for (var i = 0; i < this.count; i++) {
if(i>=start&&iend)break;
}else{
pos=current;break;
}
}
return pos;
};
/**顺序查找
* @param value 对比的数据
* @return fun 对比方法
* @return 返回位置,-1为没找到
*/
this.search=function(value,fun){
var pos=-1;
for(var i=0;i相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
移动端WEB开发中click,touch,tap事件使用详解
0
0
相关文章
javascript动画如何实现_requestAnimationFrame比setTimeout好吗?
javascript中的URL和URLSearchParams API是什么?_它们如何简化URL的处理?
javascript中生成器函数如何使用_它如何简化异步流程
JavaScript怎样实现动画效果_JavaScript中CSS动画如何结合使用
如何优化javascript的性能_为什么减少重绘和回流能提升页面速度
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具










