JavaScript鏈表結構——單向鏈表

JavaScript鏈表結構——單向鏈表

//單向鏈表類
function LinkList(){
	//節點類(data爲節點保存的數據,next爲下一個節點的引用)
	function Node(data){
		this.data = data
		this.next = null
	}
	//初始化鏈表head指向
	this.head = null
	//初始化鏈表長度(節點個數)
	this.length = 0
	
	/*
	*
	* 1. append方法,鏈表尾部增加新節點
	* 
	*/ 
	LinkList.prototype.append = function(data){
		let node = new Node(data)
		//判斷當前鏈表是否爲空
		if (this.head == null){
			this.head = node
		}
		//當前鏈表不爲空
		else{
			let current = this.head
			//查找到最後一個節點
			while(current.next != null){
				current = current.next
			}
			//使最後一個節點的next指向新節點
			current.next = node 
		}
		//增加新節點後,鏈表長度+1
		this.length += 1
	}
	/*
	*
	* 2. toString方法
	* 
	*/
	LinkList.prototype.toString = function(){
		let current = this.head
		let listString = ''
		//查找到最後一個節點
		while(current != null){
			listString += current.data + ' '
			current = current.next
		}
		return listString
	}
	/*
	*
	* 3.insert方法,指定位置插入新節點節點
	* 
	*/ 
	LinkList.prototype.insert = function(data, position){
		let node = new Node(data)
		//判斷position是否越界
		if (position < 0 || position > this.length){
			return false
		}
		//判斷插入的位置是否爲鏈表頭部
		else if (position == 0){
			node.next = this.head
			this.head = node
		}
		//插入的位置不爲鏈表頭部(current爲當前節點,previous爲前一節點)
		else{
			let index = 0
			let current = this.head
			let previous = null
			//查找到對應position的節點
			while(index < position){
				previous = current
				current = current.next
				index ++
			}
			previous.next = node
			node.next = current
		}
		//鏈表長度+1
		this.length += 1
	}
	/*
	*
	* 4.get方法,獲取指定位置的節點
	* 
	*/ 
	LinkList.prototype.get = function(position){
		//越界判斷
		if (position < 0 || position >= this.length){
			return null
		}
		let current = this.head
		let index = 0
		while(index < position){
			current = current.next
			index ++
		}
		return current.data
	}
	/*
	*
	* 5.indexOf方法,查找指定數據的索引
	* 
	*/ 
	LinkList.prototype.indexOf = function(data){
		let current = this.head
		let index = 0
		while(current != null){
			//找到data後返回索引值
			if(current.data == data){
				return index
			}
			current = current.next
			index ++
		}
		//while循環結束後沒查找到則返回-1
		return -1
	}
	/*
	*
	* 6.update方法,修改對應位置的節點的數據
	* 
	*/ 
	LinkList.prototype.update = function(newData, position){
		if (position < 0 || position >= this.length){
			return false
		}
		let current = this.head
		let index = 0
		while(index < position){
			current = current.next
			index ++ 
		}
		//打印數據變化
		console.log(current.data + '=>' + newData)
		current.data = newData
	}
	/*
	*
	* 7.removeAt方法,移除指定位置的節點
	* 
	*/ 
	LinkList.prototype.removeAt = function(position){
		if (position < 0 || position >= this.length){
			return null
		}
		//判斷移除的位置是否爲頭部
		else if(position == 0){
			this.head = this.head.next
		}
		//移除的位置不爲頭部
		else{
			let current = this.head
			let previous = null
			let index = 0
			while(index < position){
				previous = current
				current = current.next
				index ++
			}
			previous.next = current.next
		}
		//鏈表長度-1
		this.length --
	}
	/*
	*
	* 8.remove方法,根據數據移除某一節點
	* 
	*/ 
	LinkList.prototype.remove = function(data){
		//查找data對應的索引
		let index = this.indexOf(data)
		//移除指定位置的節點
		return this.removeAt(index)
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章