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 相关文章推荐
jQuery的一些特性和用法整理小结
Jan 13 Javascript
button没写type=button会导致点击时提交
Mar 06 Javascript
如何让浏览器支持jquery ajax load 前进、后退功能
Jun 12 Javascript
JavaScript 表单处理实现代码
Apr 13 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
Aug 03 Javascript
再谈javascript常见错误及解决方法
Sep 16 Javascript
JavaScript纯色二维码变成彩色二维码
Jul 23 Javascript
Angular ng-animate和ng-cookies用法详解
Apr 18 Javascript
微信小程序实现刷脸登录
May 25 Javascript
解决vue中el-tab-pane切换的问题
Jul 19 Javascript
详解JavaScript 高阶函数
Sep 14 Javascript
JavaScript枚举选择jquery插件代码实例
Nov 17 jQuery
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
example1.php
2006/10/09 PHP
php版微信返回用户text输入的方法
2016/11/14 PHP
php+Memcached实现简单留言板功能示例
2017/02/15 PHP
PHP实现的DES加密解密封装类完整实例
2017/04/29 PHP
基于JQuery框架的AJAX实例代码
2009/11/03 Javascript
快速排序 php与javascript的不同之处
2011/02/22 Javascript
jQuery内置的AJAX功能和JSON的使用实例
2014/07/27 Javascript
基于NodeJS的前后端分离的思考与实践(五)多终端适配
2014/09/26 NodeJs
JavaScript中遍历对象的property的3种方法介绍
2014/12/30 Javascript
jQuery自动完成插件completer附源码下载
2016/01/04 Javascript
基于JS实现textarea中获取动态剩余字数的方法
2016/05/25 Javascript
微信小程序 合法域名校验出错详解及解决办法
2017/03/09 Javascript
Node.Js生成比特币地址代码解析
2018/04/21 Javascript
JavaScript中创建原子的方法总结
2018/08/26 Javascript
Windows下Node爬虫神器Puppeteer安装记
2019/01/09 Javascript
vue cli3适配所有端方案的实现
2020/04/13 Javascript
jQuery实现倒计时功能完整示例
2020/06/01 jQuery
解决vue里a标签值解析变量,跳转页面,前面加默认域名端口的问题
2020/07/22 Javascript
Vue执行方法,方法获取data值,设置data值,方法传值操作
2020/08/05 Javascript
详解Django中Request对象的相关用法
2015/07/17 Python
Python 3.x 连接数据库示例(pymysql 方式)
2017/01/19 Python
Python输出带颜色的字符串实例
2017/10/10 Python
django输出html内容的实例
2018/05/27 Python
Python调用adb命令实现对多台设备同时进行reboot的方法
2018/10/15 Python
python之cv2与图像的载入、显示和保存实例
2018/12/05 Python
Scrapy框架爬取西刺代理网免费高匿代理的实现代码
2019/02/22 Python
Python后台开发Django的教程详解(启动)
2019/04/08 Python
Python3.7 pyodbc完美配置访问access数据库
2019/10/03 Python
python接口自动化之ConfigParser配置文件的使用详解
2020/08/03 Python
全球独特生活方式产品和礼品购物网站:AHAlife
2018/09/18 全球购物
买卖协议书范本
2014/04/21 职场文书
社区活动总结报告
2014/05/05 职场文书
2014年班主任工作总结
2014/11/08 职场文书
2015年个人招商工作总结
2015/04/25 职场文书
优秀员工演讲稿
2019/06/21 职场文书
如何理解及使用Python闭包
2021/06/01 Python