((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;
}));
javascript实现list
最新推荐文章于 2024-10-30 18:44:41 发布