Home Web Front-end JS Tutorial How to use JS to customize hash tables and sequential lists

How to use JS to customize hash tables and sequential lists

Jun 04, 2018 pm 01:40 PM
javascript use customize

/**哈希表*/
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;
	};
};
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of click, touch, and tap events in mobile WEB development

How to operate the page and visual area , screen width and height attributes

The above is the detailed content of How to use JS to customize hash tables and sequential lists. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

An avatar on Netflix is ​​a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles