介绍
这期内容当中小编将会给大家带来有关怎么JavaScript中实现一个双向链表,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
<强>原理如下图所示:强>
<强>示例代码:强>
/*双向链表 * */function 节点(元素),{ 时间=this.element 才能;元素; 时间=this.next 才能;零; this.previous 才能=,零;//双向链表在这里需要增加一个以前属性 } function LList (), { 时间=this.head 才能;new 节点(“head"); this.find =,才能找到; this.insert =,才能插入; 时间=this.display 才能;显示器; this.remove =,才能删除; 时间=this.findLast 才能;findLast; 时间=this.dispReverse 才能;dispReverse;//将链表反转 } function dispReverse (), { var 才能;currNode =, this.head; 时间=currNode 才能;this.findLast (); var 才能;nodestr =,““ while 才能;(! (==currNode.previous null), { ,,,nodestr +=,“,“+ currNode.element; ,,,currNode =, currNode.previous; ,,} console.log才能(“将链表反转后:,“+ nodestr); } function findLast (), { var 才能;currNode =, this.head; while 才能;(! (==currNode.next null), { ,,,currNode =, currNode.next; ,,} return 才能;currNode; } function 删除(项),{ var 才能;currNode =, this.find(项); if 才能;(! (==currNode.next null), { ,,,currNode.previous.next =, currNode.next; ,,,currNode.next.previous =, currNode.previous; ,,,currNode.next =,空; ,,,currNode.previous =,空; ,,} }//,findPrevious is no  longer 需要/* function findPrevious(项),{ ,var currNode =, this.head; ,while (! (currNode.next ==, null),,, ,(currNode.next.element !=,项),{=,,currNode  currNode.next; ,} ,return currNode; }*/function 显示器(),{ var 才能;currNode =, this.head; var 才能;nodestr =,““ while 才能;(! (==currNode.next null), { ,,,nodestr +=,“,“+ currNode.next.element; ,,,currNode =, currNode.next; ,,} console.log才能(nodestr); } function 找到(项),{ var 才能;currNode =, this.head; while 才能;(currNode.element !=,项),{ ,,,currNode =, currNode.next; ,,} return 才能;currNode; } function 插入(newElement,项),{ var 才能;newNode =, new 节点(newElement); var 才能;current =, this.find(项); 时间=newNode.next 才能;current.next; 时间=newNode.previous 才能;电流;//双向链表在这里需要设置新节点之前属性 时间=current.next 才能;newNode; } var cities =, new LList (); cities.insert (“Conway",,“head"); cities.insert (“Russellville",,“Conway"); cities.insert (“Carlisle",,“Russellville"); cities.insert (“Alma",,“Carlisle"); cities.display ();//Conway Russellville  Carlisle 阿尔玛 cities.remove (“Carlisle"); cities.display ();//Conway Russellville 阿尔玛 cities.dispReverse ();//, Alma Russellville 康威
这里使用<强>在线HTML/CSS/JavaScript代码运行工具强>:http://tools.jb51.net/code/HtmlJsRun测试上述代码,可得如下运行结果:
上述就是小编为大家分享的怎么JavaScript中实现一个双向链表了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。