前端进阶:链表的概念和应用
current = current.next i++ } previous = current.previous; next = current.next; previous.next = next; length --; return current.el } }else { return null } } }; // 移除指定节点 this.remove = (el) => { let idx = this.indexOf(el); this.removeAt(idx); }; // 查询指定位置的链表元素 this.get = (index) => { if(index < 0 || index >= length) { return undefined }else { if(length) { if(index === length - 1) { return tail.el } let current = head, i = 0; while(i < index) { current = current.next i++ } return current.el }else { return undefined } } } // 查询节点所在位置 this.indexOf = (el) => { let idx = -1, current = head, curIdx = -1; while(current) { idx++ if(current.el === el) { curIdx = idx; break; } current = current.next; } return curIdx }; // 判断链表是否为空 this.isEmpty = () => { return length === 0 }; // 返回链表长度 this.size = () => { return length }; // 将链表转化为数组返回 this.toArray = () => { let current = head, results = []; while(current) { results.push(current.el); current = current.next; } return results }; (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |