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 相关文章推荐
特殊字符、常规符号及其代码对照表
Jun 26 Javascript
jquery.ui.draggable中文文档
Nov 24 Javascript
jQuery each()小议
Mar 18 Javascript
javascript检查浏览器是否支持flash的实现代码
Aug 14 Javascript
jQuery应用之jQuery链用法实例
Jan 19 Javascript
jQuery插件Skippr实现焦点图幻灯片特效
Apr 12 Javascript
JavaScript中 ES6 generator数据类型详解
Aug 11 Javascript
react-native组件中NavigatorIOS和ListView结合使用的方法
Sep 30 Javascript
AngularJS 实现购物车全选反选功能
Oct 24 Javascript
inquirer.js一个用户与命令行交互的工具详解
May 18 Javascript
解析vue、angular深度作用选择器
Sep 11 Javascript
一篇文章带你从零快速上手Rollup
Sep 07 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中memcache 基本操作实例
2015/05/17 PHP
PHP将URL转换成短网址的算法分享
2016/09/13 PHP
PHP实现接收二进制流转换成图片的方法
2017/01/10 PHP
phpcms配置列表页以及获得文章发布时间
2017/07/04 PHP
laravel通过创建自定义artisan make命令来新建类文件详解
2017/08/17 PHP
JQuery循环滚动图片代码
2011/12/08 Javascript
js调用webservice中的方法实现思路及代码
2013/02/25 Javascript
Jquery和JS用外部变量获取Ajax返回的参数值的方法实例(超简单)
2013/06/17 Javascript
javascript一元操作符(递增、递减)使用示例
2013/08/07 Javascript
jquery中子元素和后代元素的区别示例介绍
2014/04/02 Javascript
js实现从数组里随机获取元素
2015/01/12 Javascript
JS实现网页上随滚动条滚动的层效果代码
2015/11/04 Javascript
jQuery的Cookie封装,与PHP交互的简单实现
2016/10/05 Javascript
微信小程序 MD5加密登录密码详解及实例代码
2017/01/12 Javascript
jQuery中用on绑定事件时需注意的事项
2017/03/19 Javascript
Angular2管道Pipe及自定义管道格式数据用法实例分析
2017/11/29 Javascript
JS数组去重常用方法实例小结【4种方法】
2018/05/28 Javascript
微信小程序实现弹出菜单功能
2018/06/12 Javascript
vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解
2020/04/28 Javascript
[00:28]DOTA2北京网鱼队选拔赛
2015/04/08 DOTA
python处理cookie详解
2014/02/07 Python
在Docker上部署Python的Flask框架的教程
2015/04/08 Python
浅谈Python的垃圾回收机制
2016/12/17 Python
Python中关键字nonlocal和global的声明与解析
2017/03/12 Python
Numpy掩码式数组详解
2018/04/17 Python
通过PYTHON来实现图像分割详解
2019/06/26 Python
python代码如何注释
2020/06/01 Python
西班牙网上书店:Casa del Libro
2016/11/01 全球购物
兰芝美国网上商城:购买LANEIGE睡眠面膜等
2017/06/30 全球购物
个性化皮包、小袋、生活配件:Mon Purse
2019/03/26 全球购物
美国价格实惠的在线眼镜网站:Zeelool
2020/12/25 全球购物
斯福泰克软件测试面试题
2015/02/16 面试题
人力资源部培训专员岗位职责
2014/01/02 职场文书
党员国庆节演讲稿范文2014
2014/09/21 职场文书
小学生大队委竞选稿
2015/11/20 职场文书
2017寒假社会实践心得体会范文
2016/01/14 职场文书