javascript实现数据结构(单向链表)

前言

数据结构是一种存储数据之间的逻辑结构,
合适的数据结构可以带来更高的运行效率和存储效率,
与相应解决实际问题算法的适应性也就越高,
算法和数据结构往往是互不分开的。
离开了算法,数据结构就显得毫无意义,而没有了数据结构,算法就没有实现的条件。

什么是链表

大家可以把它当成一个两边没门的老式火车,车厢就是它的内容;车厢门是它的next,它只是一个地址通向下一个车厢,一直到最后一节车厢next当然就是null啦。
随便说说有什么用,有数组为什么要用链表。
当数据量很大的时候,进行操作,数组就像是有人插队或者离队,在他后面的人都要进行后退或者前进,工作量比较大;而链表只需要把前一节车厢的门通向一个新的车厢,新车厢的门通向之前那个门后面的车厢即可,但与之对应的是访问慢,必须从车头走到车尾进行查询。

function LinkedList(){
	function Node(data){
		this.data = data
		this.next = null
	}
	
	this.head = null
	this.length = 0
	
	
	LinkedList.prototype.append = function(data){
		if(this.length == 0){
			var newNode = new Node(data)
			this.head = newNode
		}else{
			var newNode = new Node(data)
			
			var current = this.head
			while(current.next){
				current = current.next
			}
			current.next = newNode
		}
		this.length += 1
	}
	
	
	LinkedList.prototype.toString = function(){
		var current = this.head
		var ls = ''
		while(current){
			ls += current.data + ' '
			current = current.next
		}
		
		return ls
	}
	
	LinkedList.prototype.insert = function(position,data){
		if(position < 0 || position > this.length) return false
		
		var newNode = new Node(data)
		
		if(position == 0){
			//原本head指向的是第一个节点,把新节点的next接在原来第一个节点上
			newNode.next = this.head
			this.head = newNode
		}else{
			var index = 0
			//current为目标节点位置,让新节点的next指向它
			//previous为目标位置前一个节点,让这个节点的next指向新节点
			//同时也包含了插入到最后的位置
			var current = this.head
			var previous = null
			while(index++ < position){
				previous = current
				current = current.next
			}
			newNode.next = current
			previous.next = newNode
		}
		this.length += 1
		return true
	}
	
	
	LinkedList.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
	}
	
	LinkedList.prototype.indexOf = function(data){
		var  current = this.head
		var index = 0
		while(current){
			if(current.data == data) return index
			current = current.next 
			index++
		}
		return -1
	}
	
	LinkedList.prototype.updata = function(position,newData){
		position = position-1
		if(position < 0 || position > this.length-1) return false
		var current = this.head
		var index = 0
		while(index++ < position){
			current = current.next
		}
		current.data = newData
		return true
	}
	
	
	LinkedList.prototype.removeAt = function (position){
		position = position-1
		if(position < 0 || position > this.length-1 ) return false
		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
	}
	
	
	LinkedList.prototype.remove = function (data){
		var position = this.indexOf(data)+1
		return this.removeAt(position)
	}
	
	LinkedList.prototype.isEmpty = function (){
		return this.length == 0
	}
	
	LinkedList.prototype.size = function(){
		return this.length
	}
}



var ll = new LinkedList()
// 测试
ll.append('bb')
ll.append('a')
ll.append('ccc')
ll.append('eeee')
console.log(ll.toString())
console.log(ll.get(2))
console.log(ll.indexOf('a'))
ll.updata(1,'bbb')
console.log(ll.toString())
ll.removeAt(1)
console.log(ll.toString())
console.log(ll.remove('ccc'))

链接: javascript实现数据结构(双向链表).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值