Javascript 手写双向链表

Javascript 手写双向链表

代码如下
/* 
	双向链表
	append(element)  向链表的尾部插入一个节点
	insert(element,position)  向链表的指定位置插入一个节点 位置按照0,1,2,3,4,...
	get(position)  获取对应位置的元素
	indexOf(element)  返回元素在列表中的索引  如果没有则返回-1
	update(element,position)  修改某个位置的元素
	removeAt(position) 按照指定位置删除一个节点  return 被删除的节点的element
	remove(element)  按照指定元素删除一个节点  return 被删除的节点在链表中的位置
	isEmpty() 如果链表没有元素 return true  否则  return false
	size()  返回链表中节点个数
	toString() 正序返回链表中的所有元素 this.head -> Node -> Node -> ... -> this.tail
	reverseToString() 逆序返回链表的所有元素 this.tail -> Node -> Node -> ... -> this.head
*/

function LinkedList_twoWay() {
	this.head = null
	this.tail = null
	this.length = 0

	function Node(element,next=null,prev=null) {
		this.element = element
		this.next = next
		this.prev = prev
	}

	LinkedList_twoWay.prototype.append = function(element) {
		// 创建一个节点
		let node = new Node(element)
		// 判断是否是第一次插入节点
		if (this.head === null) {
			this.head = node
		} else {
			let node1 = this.head
			
			// 让node1指向最后一个节点
			while (node1.next !== null) {
				node1 = node1.next
			}

			// 最后一个节点的next 指向 新节点
			node1.next = node
			// 新节点的prev 指向 最后一个节点
			node.prev = node1
			// 链表长度+1
		}
		this.tail = node
		this.length++
	}
	LinkedList_twoWay.prototype.insert = function(element,position) {
		// 判断传入的position是否合法
		if (position > this.length) {
			return null
		}

		let node1 = this.head
		// 让node1指向到第 position 个 节点
		for (let i=0; i< position; i++) {
			node1 = node1.next
		}

		// 定义一个node 如果node不是最后一个节点再进行创建
		let node = null

		// 如果node是最后一个节点
		if (position === this.length) {
			// 执行append 并直接 return
			this.append(element)
			return
		}else {
			// 创建一个新节点
			node = new Node(element)
			// node的下一个指向node1
			node.next = node1
		}

		if (position === 0) {
			this.head = node
		}else {
			// node的上一个节点指向node1的上一个节点
			node.prev = node1.prev
			// node1的上一个节点的下一个节点指向node
			node1.prev.next = node
		}// node1的上一个节点指向node
		node1.prev = node

		this.length++
	}
	LinkedList_twoWay.prototype.get = function(position) {
		// 判断传入的position是否合法
		if (position >= this.length) {
			return null
		}
		// 让node1指向到第 position 个 节点
		let node1 = this.head
		for (let i=0; i< position; i++) {
			node1 = node1.next
		}
		// 返回相应属性
		return node1.element
	}
	LinkedList_twoWay.prototype.indexOf = function(element) {
		let node1 = this.head
		// 如果查找到了相应元素  则返回该index
		let index = 0 
		while (index < this.length) {
			if (node1.element === element) {
				return index
			}
			node1 = node1.next
			index++
		}
		return -1
	}
	LinkedList_twoWay.prototype.update = function(element,position) {
		// 判断position是否合法
		if (position >= this.length) {
			return null
		}

		let node1 = this.head
		// 将node1指向第position个节点
		for (let i=0; i< position; i++) {
			node1 = node1.next
		}

		// 修改第position个节点的element
		node1.element = element
	}
	LinkedList_twoWay.prototype.removeAt = function(position) {
		// 判断position是否合法
		if (position >= this.length) {
			return null
		}
		let node1 = this.head
		// 让node1指向第position个节点
		for (let i=0; i< position; i++) {
			node1 = node1.next
		}
		/*  
			删除过程: (假设删除节点为delNode) 
			1. 让delNode的上一个节点的next 指向 delNode的下一个节点
			2. 让delNode的下一个节点的prev 指向 delNode的上一个节点
			(下面的node1就是delNode) 
		*/
		let element = node1.element
		if (position === 0) {
			this.head = node1.next
		}else {
			// 如果删除的不是第一个节点则执行下面的语句  因为第一个节点的prev为null
			node1.prev.next = node1.next
		}
		// 如果删除的不是最后一个节点则执行下面的语句  因为最后一个节点的next为null
		if (position !== this.length - 1){
			node1.next.prev = node1.prev
		}

		this.length--

		return element
	}
	LinkedList_twoWay.prototype.remove = function(element) {
		let node1 = this.head

		let index = 0
		while (index < this.length) {
			if (node1.element === element) {
				if (index === 0) {
					this.head = node1.next
				}else {
					// 如果删除的不是第一个节点则执行下面的语句  因为第一个节点的prev为null
					node1.prev.next = node1.next
				}
				// 如果删除的不是最后一个节点则执行下面的语句  因为最后一个节点的next为null
				if (index !== this.length - 1){
					node1.next.prev = node1.prev
				}
				this.length--
				return index
			}
			node1 = node1.next
			index ++
		}
		return -1
	}
	LinkedList_twoWay.prototype.isEmpty = function() {
		return this.head === null ? true : false
	}
	LinkedList_twoWay.prototype.size = function() {
		return this.length
	}
	LinkedList_twoWay.prototype.toString = function() {
		let node1 = this.head
		let arr = []
		while(node1 !== null) {
			arr.push(node1.element)
			node1 = node1.next
		}
		return arr.join(',')
	}
	LinkedList_twoWay.prototype.reverseToString = function() {
		let node1 = this.tail
		let arr = []
		while(node1 !== null) {
			arr.push(node1.element)
			node1 = node1.prev
		}
		return arr.join(',')
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值