javascript算法-单链表
2018-07-08 12:09
155 查看
链表相比数组更具灵活性和扩展性。主要有节点数据以及指向节点的指针所构成。
链表中节点的实现【元素和指针】:
let Node = function( element ){ this.element = element; this.next = null; };
单链表的实现:
function LinkedList(){ let Node = function( element ){ this.element = element; this.next = null; }; let head = null; let length = 0; this.append = function( element ){ let newNode = new Node( element ); let current = null; if( head == null ){ head = newNode; }else{ current = head; while( current.next ) { current = current.next; } current.next = newNode; } length++; }; this.size = function(){ return length; }; this.removeAt = function( pos ){ if( pos > -1 && pos < length ){ var current = head, index = 0, prev = null; if( pos == 0 ){ head = current.next; }else{ while( index++ < pos ){ prev = current; current = current.next; } prev.next = current.next; length--; return current.element; } }else{ return null; } }; this.print = function(){ let current = head; while( current ){ console.log( current.element ); current = current.next; } }; this.insert = function( pos, element ){ let newNode = new Node( element ); let current, prev, index = 0; current = head; if( pos >= 0 && pos <= length ){ if( pos == 0 ){ newNode.next = head; head = newNode; }else{ while( index++ < pos ){ prev = current; current = current.next; } newNode.next = current; prev.next = newNode; } length++; return true; }else { return false; } }; this.toString = function(){ let current = head, string = ''; while( current ){ string += current.element + ','; current = current.next; } return string.substring( 0, string.length - 1 ); }; this.indexOf = function( element ){ let current = head, index = -1; while( current ){ index++; if( current.element == element ){ return index; } current = current.next; } return -1; }; this.remove = function( element ){ let pos = this.indexOf( element ); return this.removeAt( pos ); }; this.isEmpty = function(){ return length == 0; }; this.getHead = function(){ return head; } } var oLink = new LinkedList(); oLink.append( "java" ); oLink.append( "php" ); oLink.append( "javascript" ); oLink.append( "python" ); oLink.print(); console.log( "-----------------------节点个数------------------------") console.log( oLink.size() ); console.log( "-----------------------删除第2个元素之前------------------------") console.log( oLink.removeAt( 2 ) ); console.log( "-----------------------删除第2个元素之后------------------------") oLink.print(); console.log( "节点个数"); console.log( oLink.size() ); console.log( "-----------------------插入节点前------------------------") oLink.insert( 0, "c语言" ); oLink.insert( 3, "c++语言" ); console.log( "-----------------------插入节点后------------------------") oLink.print(); oLink.insert( 4, "erlang语言" ); console.log( "-----------------------插入节点后------------------------") oLink.print(); console.log( "-----------------------节点个数------------------------") console.log( oLink.size() ); console.log( "-----------------------toString------------------------") console.log( oLink.toString() ); console.log( "------------------------indexOf-----------------------" ); console.log( oLink.indexOf( "c语言2" ) ); console.log( "------------------------clear-----------------------" ); oLink.print();
相关文章推荐
- javascript数据结构与算法--链表
- javascript 链表(lineked lists)算法与说明
- 前端常见算法面试题之 - 从尾到头打印链表[JavaScript解法]
- 数据结构与算法JavaScript - 链表
- JavaScript数据结构与算法之链表
- JavaScript数据结构与算法Item4--链表
- JavaScript中数据结构与算法(三):链表
- JavaScript中数据结构与算法(三):链表
- 创建链表的三种算法(C语言实现)——正向、逆向、递归
- 第四周-项目二—建设“单链表”算法库
- 算法————笔试内容--->链表(二)
- 第4周项目2 建设“单链表”算法库
- 顺序表应用1:多余元素删除之移位算法(链表实现)
- 第4周项目2-建立“单链表”算法库
- javascript实现的gzip压缩(deflate)和解压缩(inflate)算法 - sudone.com服务器系统架构分析日志
- 8---------单链表算法的简单应用和巩固
- javascript数组的常见算法
- 算法学习之数据结构之链表是否相交,链表是否存在环
- JavaScript 面试中常见算法问题详解
- JavaScript算法教程之sku(库存量单位)详解