【javascript】數據結構-鏈表

// 建立一個鏈表
function LinkedList(){

	// 建立一個Node輔助類,表示須要加入列表的項,它包含一個element屬性,即表示須要加入到列表中的值,next屬性表示指向下一個節點項目的指針
	let Node = function(element){
		this.element = element;
		this.next = null;
	};

	// 長度初始化爲0,列表頭部初始化爲空
	let length = 0;
	let head = null;

	// append方法,向鏈表尾部追加元素
	this.append = function(element){
		let node = new Node(element),
			current;
		
		// 列表中的第一個節點
		if(head == null){
			head = node;
		}

		else{
			current = head;
			// 循環列表,直到找到最後一項
			while(current.next){
				current = current.next;
			}
			// 找到最後一項,將next值賦給node,創建連接
			current.next = node;
		}
		// 更新列表長度
		length++;
	};

	// insert方法,向列表的特定位置插入一個新的項
	this.insert = function(position, element){
		// 檢查越界值
		if(position >=0 && position <=length){

			// current是對插入元素以後的元素的引用,previous是對插入元素以前的元素的引用
			let node = new Node(element),
				current = head,
				previous,
				index = 0;

			// 在第一項位置添加
			if(position === 0){
				node.next = current;
				head = node;
			}

			else{
				while(index++ < position){
					previous = current;
					current = current.next;
				}
				node.next = current;
				previous.next =node;
			}
			// 更新列表長度
			length++;
			return true;
		}
		else{
			return false;
		}
	};

	// removeAt方法:從鏈表特定位置移除一項
	this.removeAt = function(position){
		// 檢查越界值
		if(position > -1 && position < length){
			let current = head,
				previous,
				index = 0;

			// 移除第一項
			if(position === 0){
				head = current.next;
			}

			else{
				while(index++ < position){
					previous = current;
					current = current.next;
				}
				// 將previous與current的下一項連接起來,跳過current從而移除它
				previous.next = current.next;
			}
			// 更新列表長度
			length--;
			return current.element;
		}
		else{
			return null;
		}
	};

	// remove方法:從列表中移除一項
	this.remove = function(element){
		let index = this.indexOf(element);
		// 調用removeAt()方法
		return this.removeAt(index);
	};

	// indexOf方法:返回元素在列表中的索引,若是沒有該元素則返回-1;
	this.indexOf = function(element){
		let current = head,
			index = 0;

		while(current){
			if(element === current.element){
				return index;
			}
			index++;
			current = current.next;
		}

		return -1;
	};

	// isEmpty方法,若是鏈表中不包含任何元素,則返回true,不然返回false
	this.isEmpty = function(){
		return length === 0;
	};

	// size方法,返回鏈表中包含的元素個數,與數組的length屬性相似
	this.size = function(){
		return length;
	};

	// getHead方法:返回鏈表第一個元素
	this.getHead = function(){
		return head;
	};

	// toSting方法,因爲鏈表使用了Node類,重寫了javascript的toString方法,讓其只輸出元素的值
	this.toString = function(){
		let current = head,
			string = '';

		while(current){
			string += current.element + (current.next ? ',':'');
			current = current.next;
		}
		return string;
	};

	// print方法,用於在控制檯輸出鏈表元素
	this.print = function(){
		console.log(this.toString());
	};
}

// 鏈表的使用
var lnkst = new LinkedList();

// 打印鏈表長度
console.log(lnkst.size());

// 給鏈表添加元素
lnkst.append(1);
lnkst.append(2);
lnkst.append(3);
lnkst.append(4);
lnkst.append(5);

// 調用打印方法
lnkst.print();			//輸出1,2,3,4,5

// 插入元素
lnkst.insert(2,'a');
lnkst.print();			//輸出1,2,a,3,4,5

// 按位置刪除元素
lnkst.removeAt(2);
lnkst.print();			//輸出1,2,3,4,5

// 按值刪除元素
lnkst.remove(5);
lnkst.print();			//輸出1,2,3,4

//判斷是否爲空
console.log(lnkst.isEmpty());		//false

// 獲取頭部;
console.log(lnkst.getHead());		//1
相關文章
相關標籤/搜索