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

 更新时间:2020年09月17日 11:43:33   作者:TanJia  
这篇文章主要介绍了JavaScript如何封装单向链表,帮助大家更好的理解和使用JavaScript,感兴趣的朋友可以了解下

使用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封装单向链表的资料请关注脚本之家其它相关文章!

相关文章

  • uniapp movable-area应用

    uniapp movable-area应用

    这篇文章主要为大家介绍了uniapp movable-area应用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • js微信应用场景之微信音乐相册案例分享

    js微信应用场景之微信音乐相册案例分享

    这篇文章主要为大家分享了js微信应用场景之微信音乐相册案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • 利用Javascript实现简单的转盘抽奖

    利用Javascript实现简单的转盘抽奖

    这篇文章主要介绍了利用Javascript实现的简单的转盘抽奖,文中分享了两种抽奖效果,一种是默认转动,一种是需要点击开始转动的,并给出了晚上的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • canvas压缩图片转换成base64格式输出文件流

    canvas压缩图片转换成base64格式输出文件流

    本文主要介绍了canvas压缩图片转换成base64格式输出文件流的方法,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Bootstrap每天必学之导航组件

    Bootstrap每天必学之导航组件

    Bootstrap每天必学之导航组件,制作面包屑式导航、导航加下拉菜单效果,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • 微信小程序模拟cookie的实现

    微信小程序模拟cookie的实现

    本篇文章主要介绍了微信小程序模拟 cookie的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • js 实现 list转换成tree的方法示例(数组到树)

    js 实现 list转换成tree的方法示例(数组到树)

    这篇文章主要介绍了js 实现 list转换成tree的方法示例(数组到树),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • CocosCreator入门教程之用TS制作第一个游戏

    CocosCreator入门教程之用TS制作第一个游戏

    这篇文章主要介绍了CocosCreator入门教程之用TS制作第一个游戏,对TypeScript感兴趣的同学,一定要看一下
    2021-04-04
  • 深入理解Javascript里的依赖注入

    深入理解Javascript里的依赖注入

    我喜欢引用这句话,“程序是对复杂性的管理”。计算机世界是一个巨大的抽象建筑群。我们简单的包装一些东西然后发布新工具,周而复始。现在思考下,你所使用的语言包括的一些内建的抽象函数或是低级操作符。这在JavaScript里是一样的
    2014-03-03
  • 使用JavaScript获取Request中参数的值方法

    使用JavaScript获取Request中参数的值方法

    下面小编就为大家带来一篇使用JavaScript获取Request中参数的值方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09

最新评论