JS数据结构(四)——链表

130 阅读2分钟

一:单链表

//创建链表
function LinkedList() {
	let Node = function(element) {
		this.element = element;
		this.next = null;
	};
	let length = 0;
	let head = null;

	//向链表尾部追加元素
	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++;  //更新长度
	}

	//从链表中移除元素,两种场景:第一种是移除第一个元素;第二种是移除第一个以外的任一元素
	//从特定位置移除一个元素:
	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;
		}
	}


	//在任意位置插入元素
	this.insert = function(position, element){
		//检查越界值
		if(position >= 0 && position < length){
			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;
		}
	}

	//转换成字符串
	this.toString = function() {
		let current = head, string = '';
		while (current) {
			string += current.element;
			current = current.next;
		}
		return string;
	}

	//元素的位置
	this.indexOf = function(element) {
		let current = head, index = -1;
		while(current) {
			if(element == current.element){
				return index;
			}
			index++;
			current = current.next;
		}
		return -1;
	}

	//是否为空
	this.isEmpty = function() {
		return length == 0;
	}

	//大小
	this.size = function() {
		return length;
	}

	//获取头部
	this.getHead = function() {
		return head;
	}

}

二:双链表

function DoublyLinkedList() {
	let Node = function(element) {
		this.element = element;
		this.next = null;
		this.prev = null;
	};
	let length = 0;
	let head = null;
	let tail = null;

	//插入元素
	this.insert = function(position, element){
		if(position >= 0 && position <= length){
			let node = new Node(element),
			current = head,
			previous,
			index = 0;

			if(position == 0){
				if(!head){
					head = node;
					tail = head;
				}else{
					node.next = current;
					current.prev = node;
					head = node;
				}
			}else if(position == length){
				current = tail;
				current.next = node;
				node.prev = current;
				tail = node;
			}else{
				while(index++ < position){
					previous = current;
					current = current.next;
				}
				previous.next = node;
				node.prev = previous;
				node.next = current;
				current.prev = node;
			}
			length++;
			return true;
		}else{
			return false;
		}
	}

	//移除元素
	this.removeAt = function(position){
		if(position > -1 && position < length){
			let current = head,
			previous,
			index = 0;

			if(position == 0){
				head = current.next;
				if(length == 1){
					tail = null;
				}else{
					head.prev = null;
				}
			}else if(position == length-1){
				current = tail;
				tail = current.prev;
				tail.next =null;
			}else{
				while(index++ < position){
					previous = current;
					current = current.next;
				}
				previous.next = current.next;
				current.next.prev = previous;
			}
			length--;
			return current.element;
		}else{
			return null;
		}
	};
}