javascript算法-单链表
链表相比数组更具灵活性和扩展性。主要有节点数据以及指向节点的指针所构成。
链表中节点的实现【元素和指针】:
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();
作者:ghostwu, 出处:http://www.cnblogs.com/ghostwu
博客大多数文章均属原创,欢迎转载,且在文章页面明显位置给出原文连接
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架