javascript实现list

本文介绍了一种使用JavaScript实现的双向链表结构。通过构造函数和节点类定义,实现了链表的基本操作,如添加、删除元素,获取元素索引等,并提供了链表转数组、遍历及打印等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

((function (factory) {
	//使用了amd模块化编程的情况下,定义名称为List的module,在依赖了List的module中使用new List()来创建链表
	if (typeof define === "function" && define.amd) {
		define(factory);
	} else {
	//未使用amd模块化编程的情况下,在window进行下全局定义,任何地方都可以使用new List()来创建链表
		window.List = factory();
	}
})(function () {
	/**
	 * 链表节点
	 *
	 * @param element 元素对象
	 */
	var node = function (element) {
		this.element = element;
		this.next = null;
		this.prev = null;
	};
	/**
	 * 构造方法
	 */
	var List = function () {
		var d = new Date();
		this.head = new node("_jslist_head" + d.getTime());
		this.end = new node("_jslist_end" + d.getTime());
		this.head.next = this.end;
		this.end.prev = this.head;
		/**
		 * 添加元素
		 *
		 * @param element 元素对象
	 	 */
		this.add = function (element) {
			var lastNode = this.end.prev, newNode = new node(element);
			lastNode.next = newNode;
			newNode.prev = lastNode;
			newNode.next = this.end;
			this.end.prev = newNode;
		};
		/**
		 * 移除元素
		 *
		 * @param element 元素对象
		 * @throws 当list为空时抛出异常
	 	 */
		this.remove = function (e) {
			if (this.size() == 0) {
				throw new Error("The List is empty!");
			} else {
				var tempNode = this.head;
				while (tempNode.next != null) {
					if (tempNode.prev != null && tempNode.next != null) {
						if (tempNode.element === e) {
							tempNode.prev.next = tempNode.next;
							tempNode.next.prev = tempNode.prev;
						}
					}
					tempNode = tempNode.next;
				}
			}
		};
		/**
		 * 统计元素个数
		 *
		 * @return
	 	 */
		this.size = function () {
			var tempNode = this.head, size = 0;
			while (tempNode.next != this.end) {
				size++;
				tempNode = tempNode.next;
			}
			return size;
		};
		/**
		 * 获取元素在链表中的索引位置,如果链表中不存在该元素则返回-1
		 *
		 * @param element 元素对象
		 * @return
	 	 */
		this.indexOf = function (element) {
			var tempNode = this.head, index = -1, j = -1;
			while (tempNode.next != null) {
				if (tempNode.prev != null && tempNode.next != null) {
					if (tempNode.element === element) {
						index = j;
						break;
					}
				}
				j++;
				tempNode = tempNode.next;
			}
			return index;
		};
		/**
		 * 获取元素在链表中的索引位置,返回true或false
		 *
		 * @param element 元素对象
		 * @return
	 	 */
		this.contains = function (e) {
			var tempNode = this.head, isExists = false;
			while (tempNode.next != null) {
				if (tempNode.prev != null && tempNode.next != null) {
					if (tempNode.element === e) {
						isExists = true;
						break;
					}
				}
				tempNode = tempNode.next;
			}
			return isExists;
		};
		/**
		 * 获取指定位置的元素
		 *
		 * @param index 索引
		 * @throws 当list为空时抛出异常
		 * @throws 当index大于list的最大index时抛出异常
		 * @throws 当index小于0时抛出异常
		 * @return
	 	 */
		this.get = function (index) {
			var maxIndex = this.size() - 1;
			if (this.size() == 0) {
				throw new Error("This List is Empty!");
			} else {
				if (index > maxIndex) {
					throw new Error("Out of size!");
				} else {
					if (index < 0) {
						throw new Error("The argument must be greater than 0!");
					} else {
						var tempNode = this.head, j = -1;
						while (tempNode.next != null) {
							if (tempNode.prev != null && tempNode.next != null) {
								if (j == index) {
									return tempNode.element;
								}
							}
							j++;
							tempNode = tempNode.next;
						}
					}
				}
			}
		};
		/**
		 * 改变指定位置元素的值
		 *
		 * @param element 元素对象
		 * @param index 索引
		 * @throws 当list为空时抛出异常
		 * @throws 当index大于list的最大index时抛出异常
		 * @throws 当index小于0时抛出异常
	 	 */
		this.set = function (e, index) {
			var maxIndex = this.size() - 1;
			if (this.size() == 0) {
				throw new Error("This List is Empty!");
			} else {
				if (index > maxIndex) {
					throw new Error("Out of size!");
				} else {
					if (index < 0) {
						throw new Error("The argument must be greater than 0!");
					} else {
						var tempNode = this.head, j = -1;
						while (tempNode.next != null) {
							if (tempNode.prev != null && tempNode.next != null) {
								if (j == index) {
									tempNode.element = e;
								}
							}
							j++;
							tempNode = tempNode.next;
						}
					}
				}
			}
		};
		/**
		 * 将链表转化为数组(所有类型相同才可以转化)
		 *
		 * @throws 当链表中的元素存在类型不尽相同时抛出异常
		 * @return 
		 */
		this.toArray = function () {
			var tempNode = this.head, _array = [];
			if (this.size() >= 0) {
				var lastType = typeof (this.end.prev.element);
				while (tempNode.next != null) {
					if (tempNode.prev != null && tempNode.next != null) {
						if (typeof tempNode.element == lastType) {
							_array.push(tempNode.element);
						} else {
							throw new Error("Some type of element are defferent!");
						}
					}
					tempNode = tempNode.next;
				}
			}
			return _array;
		};
		/**
		 * 改变指定位置元素的值
		 *
		 * @param function 对象操作函数
		 * @throws 当操作函数operator的类型不是function时抛出异常
	 	 */
		this.each = function (operator) {
			if (typeof operator != "function") {
				throw new Error("The argument must be type of function!");
			} else {
				var tempNode = this.head, index = -1;
				while (tempNode.next != null) {
					if (tempNode.prev != null && tempNode.next != null) {
						operator(index, tempNode.element);
					}
					index++;
					tempNode = tempNode.next;
				}
			}
		};
		/**
		 * 打印所有元素到控制到console
	 	 */
		this.print = function () {
			var tempNode = this.head;
			while (tempNode.next != null) {
				if (tempNode.prev != null && tempNode.next != null) {
					console.log(tempNode.element);
				}
				tempNode = tempNode.next;
			}
		};
	};
	return List;
}));




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值