JavaScript封装单向链表的示例代码


Posted in Javascript onSeptember 17, 2020

使用JavaScript封装单向链表:

1. 封装LinkList的类,用于表示我们的链表结构。

2. 在LinkList类中有一个Node类,用于封装每一个节点上的信息(data与next)。

3. 在链表中保存两个属性,一个是链表的长度,一个是链表中的第一个节点。

4.封装一些链表的常用方法:

  • append(element):想列表尾部添加一个新的项;
  • insert(position,element):向列表的特定位置插入一个新的项;
  • get(position):获取对应位置的元素;
  • indexOf(element):返回元素在链表中的索引,如果链表中没有该元素则返回-1;
  • update(position,element):修改某个位置的元素;
  • removeAt(postion):从列表的特定位置移除一项;
  • remove(element):从列表中移除一项;
  • isEmpty():如果链表中不包含任何元素,返回true,否则返回false;
  • size():返回链表中包含元素的个数;
  • toString():输出链表元素的值;
<script type="text/javascript">
	function LinkList(){
		/* 节点类 */
		function Node(data){
			this.data = data
			this.next = null
		}
		
		this.head = null
		this.length = 0
		/* 追加方法 */
		LinkList.prototype.append = function(data){
			/* 创建新节点 */
			var newNode = new Node(data)
			if(this.length === 0){
				this.head = newNode
			}else{
				/* 找到最后一个节点 */
				var current = this.head
				while(current.next){
					current = current.next
				}
				current.next = newNode
			}
			this.length += 1
		}

		/* toString方法 */
		LinkList.prototype.toString = function(){
			var current = this.head
			var listString = ""
			
			while(current){
				listString += current.data +" "
				current = current.next
			}
			return listString
		}

		/* insert方法 */
		LinkList.prototype.insert = function(position,data){
			/* 对position进行越界判断 */
			if(position<0||position>this.length) return false
			var node = new Node(data)
			if(position == 0){
				node.next = this.head
				this.head = node
			}else{
				var index = 0
				var current = this.head
				var previous = null
				while(index++ < position){
					previous = current
					current = current.next
				}
				node.next = current
				previous.next = node
			}
			this.length += 1
			return true
		}
		
		/* get方法 */
		LinkList.prototype.get = function(position){
			/* 越界判断 */
			if(position<0 || position >= this.length) return null
			
			var current = this.head
			var index = 0
			while(index++ < position){
				current = current.next
			}
			return current.data
		}

		/* indexOf方法 */
		LinkList.prototype.indexOf = function(data){
			/* 定义变量 */
			var current = this.head
			var index = 0
			/* 开始查找 */
			while(current){
				if(current.data === data){
					return index
				}else{
					current = current.next
					index += 1
				}
			}
			return -1
		}

		/* update方法 */
		LinkList.prototype.update = function(position,data){
			/* 越界判断 */
			if(position<0 || position >= this.length) return false
			
			var current = this.head
			var index = 0
			while(index++ < position){
				current = current.next
			}
			/* 修改data */
			current.data = data
			return true
		}

		/* removeAt方法 */
		LinkList.prototype.removeAt = function(position){
			/* 越界判断 */
			if(position<0 || position >= this.length) return null
			var current = this.head
			if(position === 0){
				this.head = this.head.next
			}else{
				var index = 0
				var previous = null
				while(index++ < position){
					previous = current
					current = current.next
				}
				previous.next = current.next
			}
			this.length -= 1
			return current.data
		}
		
		/* remove */
		LinkList.prototype.remove = function(data){
			/* 根据data找位置 */
			var position = this.indexOf(data)
			return this.removeAt(position)
		}
		
		LinkList.prototype.isEmpty = function(){
			return this.length === 0
		}
		
		LinkList.prototype.size = function(){
			return this.length
		}
		
	}
	
	
	/* 测试 */
	var list = new LinkList()
	list.append('a')
	list.append('b')
	list.append('c')
	console.log(list.toString()) /* a b c */
	
	list.insert(3,'d')
	console.log(list.toString())/* a b c d */
	
	console.log(list.get(2)) /* c */
	console.log(list.indexOf('d')) /* 3 */
	
	list.update(1,'bbb')
	console.log(list.toString()) /* a bbb c d */
	
	console.log(list.removeAt(2)) /* c */
	console.log(list.toString())/* a bbb d */
	
	console.log(list.remove('a'))
	console.log(list.toString())/* bbb d */
	
	console.log(list.isEmpty()) /* false */
	
	console.log(list.size()) /* 2 */
</script>

以上就是JavaScript封装单向链表的示例代码的详细内容,更多关于JavaScript封装单向链表的资料请关注三水点靠木其它相关文章!

Javascript 相关文章推荐
JS无限树状列表实现代码
Jan 11 Javascript
jqPlot 图表中文API使用文档及源码和在线示例
Feb 07 Javascript
javascript获取隐藏dom的宽高 具体实现
Jul 14 Javascript
JavaScript函数的4种调用方法详解
Apr 22 Javascript
JavaScript让网页出现渐隐渐显背景颜色的方法
Apr 21 Javascript
jQuery实现的Div窗口震动效果实例
Aug 07 Javascript
Sortable.js拖拽排序使用方法解析
Nov 04 Javascript
JS设置CSS样式的方式汇总
Jan 21 Javascript
JavaScript评论点赞功能的实现方法
Mar 13 Javascript
node.js连接MongoDB数据库的2种方法教程
May 17 Javascript
详解js常用分割取字符串的方法
May 15 Javascript
vue限制输入框只能输入8位整数和2位小数的代码
Nov 06 Javascript
vue修改Element的el-table样式的4种方法
Sep 17 #Javascript
vue+canvas实现拼图小游戏
Sep 18 #Javascript
JavaScript 常见的继承方式汇总
Sep 17 #Javascript
JavaScript 闭包的使用场景
Sep 17 #Javascript
javascript贪吃蛇游戏设计与实现
Sep 17 #Javascript
js实现简单的随机点名器
Sep 17 #Javascript
谈谈JavaScript中的垃圾回收机制
Sep 17 #Javascript
You might like
php 随机排序广告的实现代码
2011/05/09 PHP
解析PHP中的file_get_contents获取远程页面乱码的问题
2013/06/25 PHP
使用PHP接收POST数据,解析json数据
2013/06/28 PHP
php返回字符串中所有单词的方法
2015/03/09 PHP
PHP的命令行命令使用指南
2015/08/18 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
PHP 7.1中AES加解密方法mcrypt_module_open()的替换方案
2017/10/17 PHP
JS 获取span标签中的值的代码 支持ie与firefox
2009/08/24 Javascript
jQuery总体架构的理解分析
2011/03/07 Javascript
通过js动态操作table(新增,删除相关列信息)
2012/05/23 Javascript
jquery检测input checked 控件是否被选中的方法
2014/03/26 Javascript
JavaScript错误处理
2015/02/03 Javascript
jQuery Validate初步体验(一)
2015/12/12 Javascript
Bootstrap开关(switch)控件学习笔记分享
2016/05/30 Javascript
JS for循环中i++ 和 ++i的区别介绍
2016/07/20 Javascript
浅谈JS中的bind方法与函数柯里化
2016/08/10 Javascript
详解Angular Reactive Form 表单验证
2017/07/06 Javascript
vue一个页面实现音乐播放器的示例
2018/02/06 Javascript
vue2.0学习之axios的封装与vuex介绍
2018/05/28 Javascript
Python实现新浪博客备份的方法
2016/04/27 Python
python实现随机梯度下降(SGD)
2020/03/24 Python
Python科学计算包numpy用法实例详解
2018/02/08 Python
示例详解Python3 or Python2 两者之间的差异
2018/08/23 Python
Python post请求实现代码实例
2020/02/28 Python
Canvas多边形绘制的实现方法
2019/08/05 HTML / CSS
Html5与App的通讯方式详解
2019/10/24 HTML / CSS
奥巴马英文演讲稿
2014/05/15 职场文书
大学生村官考核材料
2014/05/23 职场文书
加油口号大全
2014/06/13 职场文书
2014年物业公司工作总结
2014/11/22 职场文书
财务部会计岗位职责
2015/02/03 职场文书
2015年公务员个人工作总结
2015/04/24 职场文书
超强台风观后感
2015/06/09 职场文书
教师节校长致辞
2015/07/31 职场文书
Python标准库之typing的用法(类型标注)
2021/06/02 Python
Golang解析JSON对象
2022/04/30 Golang