JS自定義哈希表和順序列表

/**哈希表*/
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<index){
			this.list[index]=obj;
			return true;
		}else return false;
	};
	/**獲得對象
	* @param index 索引位
	* @return obj 對象
	*/
	this.get=function(index){
	    return this.list[index];
	};
	/**刪除對象
	* @param index 索引位
	* @return obj 對象
	*/
	this.remove=function(index){
	    var obj=null;
	    if(index>=0&&index<this.count){
	  	    obj = this.list[index];
            for (var i = index; i < this.count - 1; i++) {
                this.list[i] = this.list[i + 1];
            }
            this.list[this.count-1] = null;
            this.count-=1; 
	    }
	    return obj;
	};
	/**刪除一組對象
	* @param index  索引位
	* @param length 長度
	*/
	this.removeGroup=function(index,length){
	    if(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&&i<end)continue;
        	    lists[lists.length] = this.list[i];
        	    counts++;
            }
            this.list=lists;
            this.count=counts; 
	    }
	};
	/**清空所有對象*/
	this.clear=function(){
	    this.count=0;   
	    this.list=[];
	};
	/**獲得鏈表大小
	* @return 返回大小
	*/
	this.size=function(){
	    return this.count;
	};
	this.getList=function(){
		return this.list;
	};
	this.setList=function(list){
		this.list=list;
		this.count=list.length;
	};
	/**折半查找
	* @param value 對比的數據
	* @return fun  對比方法
	* @return 返回位置,-1爲沒找到
	*/
	this.bisearch=function(value,fun){
	   var pos=-1;
       var start = 0;
       var end = this.count-1;
       var current = parseInt(this.count/2);
       for(var i=0;i<current;i++){
       	 if(!fun(value,this.list[start])){
       		start =start+1;
       	 }else{
       	 	pos=start;break;
       	 }
       	 if(!fun(value,this.list[current])){
       		current =current+1;if(current>end)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<this.count;i++){
       	 if(fun(value,this.list[i])){
       	 	pos=i;break;
       	 }
       }
       return pos;
	};
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章