双向链表,
双向链表需要增加一个previous属性
/*双向链表
* */
function Node(element) {
this.element = element;
this.next = null;
this.previous = null;//双向链表在这里需要增加一个previous属性
} function LList() {
this.head = new Node("head");
this.find = find;
this.insert = insert;
this.display = display;
this.remove = 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 remove(item) {
var currNode = this.find(item);
if (!(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
} // findPrevious is no longer needed
/*function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) &&
(currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}*/ function display() {
var currNode = this.head;
var nodestr = "";
while (!(currNode.next == null)) {
nodestr += " "+currNode.next.element;
currNode = currNode.next;
}
console.log(nodestr);
} function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
}
return currNode;
} function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
newNode.previous = current;//双向链表在这里需要设置新节点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 Alma
cities.remove("Carlisle");
cities.display();//Conway Russellville Alma
cities.dispReverse();// Alma Russellville Conway

js实现双向链表, 双向链表需要增加一个previous属性的更多相关文章

  1. JS判断浏览器是否支持某一个CSS3属性

    1.引子 css3的出现让浏览器的表现更加的丰富多彩,表现冲击最大的就是动画了,在日常书写动画的时候,很有必要去事先判断浏览器是否支持,尤其是在写CSS3动画库的时候.比如transition的ani ...

  2. JS判断浏览器是否支持某一个CSS3属性的方法

    var div = document.createElement('div'); console.log(div.style.transition); //如果支持的话, 会输出 "&quo ...

  3. 如何在属性面板中增加一个属性-UI界面编辑器(XproerUI)教程

    版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...

  4. JS数组 团里添加新成员(向数组增加一个新元素)只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素。myarray[5]=88;

    团里添加新成员(向数组增加一个新元素) 上一节中,我们使用myarray变量存储了5个人的成绩,现在多出一个人的成绩,如何存储呢?  只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素. my ...

  5. js数组常用操作方法小结(增加,删除,合并,分割等)

    本文实例总结了js数组常用操作方法.分享给大家供大家参考,具体如下: var arr = [1, 2, 3, 4, 5]; //删除并返回数组中第一个元素 var theFirst = arr.shi ...

  6. CKEditor在线编辑器增加一个自定义插件

    CKEditor是一个非常优秀的在线编辑器,它的前身就是FCKEditor,CKEditor据官方说是重写了内核的,但功能和性能比FCKEditor更为强大和优越.记得07年的时候第一次接触FCKEd ...

  7. 【轮子狂魔】手把手教你用JS给博客动态增加目录 - 超级懒人版

    动态显示目录的作用 不用每次写博客的时候繁琐的人工整理目录,又可以动态浮动在右下角,方便快速跳到感兴趣的位置同时也可以快速的对文章内容有一个大概的了解. 实现原理 首先根据个人喜好,我习惯了用 h1 ...

  8. 开源框架.netCore DncZeus学习(三)增加一个菜单

    框架运行起来了,先尝试增加一个菜单. 本节增加一个菜单名字:公司管理,需要注意一点,所有的name都要保持一致,注意圈中部分.为了防止手敲代码出错,建议复制已有的代代码进行修改(比如这里用的Role页 ...

  9. Rails 增加一个模型(model)

      之前我们已经看到用脚手架运行的model程序.现在是时候第二个model了. 第二个model用来处理post的评论. 7.1 新建一个模型 Rails模型使用一个单一的的名称,其相应的数据库表使 ...

随机推荐

  1. 2018.06.30 BZOJ3083: 遥远的国度(换根树剖)

    3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 512 MB Description 描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国 ...

  2. 自学如何去学习jQuery

    学习JQ第一个demo: 制作一个轮播图,制作方法我前面写了一篇博客,传送门-->http://www.cnblogs.com/yewenxiang/p/6100206.html 需要的JQ知识 ...

  3. $clog2(转)

    (转http://www.xilinx.com/support/answers/44586.html) 13.2 Verilog $clog2 function implemented imprope ...

  4. 20155216 2016-2017-2 《Java程序设计》第六周学习总结

    20155216 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 流与IO 将数据从来源中取出,可以使用输入串流:将数据写入目的地,可以使用输出串流,串流是有 ...

  5. java使用WebUploader做大文件的分块和断点续传

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  6. 12) maven-compiler-plugin

    The Compiler Plugin is used to compile the sources of your project. At present the default source se ...

  7. 几个CSS-content的小例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. java web前端easyui(layout+tree+双tabs)布局+树+2个选项卡tabs

    1.列出要实现的样式: 2.实现的代码: 分三大部分: 1):页面主体部分:mian.vm <html> <head> <title>Ks UI</title ...

  9. SQL Server 索引基本概念与优化

    数据页和区 页 SQL Server 中的数据以“页”(Page)的形式保存数据,页是SQL Server 的IO单位,读/写一次至少是一页.一页为8K(8192byte). 页由三部分组成,页头,数 ...

  10. 一起学习MVC(4)Controllers的学习

                控制器Controllers Controllers为控制器文档,AccountControllers内的方法对应View→Account下的cshtml文件. 我们看到Aco ...