链表
// 单向链表 function LinkedList() { this.head = {element: 'head', next: null}; this.node = function (node) {return {element: node, next: null}}; // 查找 this.find = function (node) { var currNode = this.head; while (currNode !== null && currNode.element !== node) { currNode = currNode.next; } return currNode; }; // 输出 this.show = function () { var currNode = this.head; while (currNode.next !== null) { currNode = currNode.next; console.log(currNode.element); } }; // 插入 this.insert = function (newNode, targetNode) { var newNode = this.node(newNode); var targetNode = this.find(targetNode); newNode.next = targetNode.next; targetNode.next = newNode; }; // 上一个节点 this.prev = function (node) { var currNode = this.head; while (currNode.next !== null && currNode.next.element !== node) { currNode = currNode.next; } return currNode; }; // 删除 this.remove = function (node) { var prevNode = this.prev(node); prevNode.next = prevNode.next.next; }; }