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 相关文章推荐
微信小程序 火车票查询实例讲解
Oct 17 Javascript
JS去除字符串中空格的方法
Feb 14 Javascript
ReactJs实现树形结构的数据显示的组件的示例
Aug 18 Javascript
常用的9个JavaScript图表库详解
Dec 19 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
Jan 12 Javascript
用jquery获取select标签中选中的option值及文本的示例
Jan 25 jQuery
Vue.js 实现微信公众号菜单编辑器功能(二)
May 08 Javascript
vue组件中的样式属性scoped实例详解
Oct 30 Javascript
vue 移动端适配方案详解
Nov 15 Javascript
微信小程序中button去除默认的边框实例代码
Aug 01 Javascript
js实现飞机大战小游戏
Aug 26 Javascript
vue+echarts+datav大屏数据展示及实现中国地图省市县下钻功能
Nov 16 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实现抓取HTTPS内容
2014/12/01 PHP
微信公众平台开发实现2048游戏的方法
2015/04/15 PHP
SyntaxHighlighter代码加色使用方法
2008/09/07 Javascript
关于jQuery $.isNumeric vs. $.isNaN vs. isNaN
2013/04/15 Javascript
一个不错的字符串转码解码函数(自写)
2014/07/31 Javascript
JavaScript中的函数声明和函数表达式区别浅析
2015/03/27 Javascript
flash+jQuery实现可关闭及重复播放的压顶广告
2015/04/15 Javascript
jQuery预加载图片常用方法
2015/06/15 Javascript
详解JavaScript的流程控制语句
2015/11/30 Javascript
浅析Javascript匿名函数与自执行函数
2016/02/06 Javascript
JS给Array添加是否包含字符串的简单方法
2016/10/29 Javascript
基于Vue2.0+ElementUI实现表格翻页功能
2017/10/23 Javascript
vue-cli webpack2项目打包优化分享
2018/02/07 Javascript
详解Vue 多级组件透传新方法provide/inject
2018/05/09 Javascript
我要点爆”微信小程序云开发之项目建立与我的页面功能实现
2019/05/26 Javascript
vue-router路由懒加载及实现的3种方式
2021/02/28 Vue.js
使用Python下载Bing图片(代码)
2013/11/07 Python
Windows和Linux下Python输出彩色文字的方法教程
2017/05/02 Python
浅谈python中str字符串和unicode对象字符串的拼接问题
2018/12/04 Python
Python利用requests模块下载图片实例代码
2019/08/12 Python
Python Django框架防御CSRF攻击的方法分析
2019/10/18 Python
Python3基本输入与输出操作实例分析
2020/02/14 Python
如何教少儿学习Python编程
2020/07/10 Python
Ubuntu权限不足无法创建文件夹解决方案
2020/11/14 Python
潘多拉珠宝俄罗斯官方网上商店:PANDORA俄罗斯
2020/09/22 全球购物
出纳员的岗位职责
2014/02/22 职场文书
父母对孩子说的话
2014/04/12 职场文书
小学生期末评语大全
2014/04/21 职场文书
雷人标语集锦
2014/06/19 职场文书
部队反四风对照检查材料
2014/09/26 职场文书
学校领导班子四风对照检查材料
2014/09/27 职场文书
教师个人查摆剖析材料
2014/10/14 职场文书
小爸爸观后感
2015/06/15 职场文书
公司中层管理培训心得体会
2016/01/11 职场文书
Html5调用企业微信的实现
2021/04/16 HTML / CSS
A22国内电台短波广播频率表
2022/05/10 无线电