上实战代码:

linkedlistnode.js 节点类

/*
* 链表节点
*/
Dare.LinkedListNode = function () {
  this.data = null;//数据域
  this.prev = null;//前驱
  this.next = null;//后驱
};
Dare.extend(Dare.LinkedListNode, Dare);
Dare.LinkedListNode.prototype.getValue = function () {
  return this.data;
};
Dare.LinkedListNode.prototype.setValue = function (obj) {
  this.data = obj;
};
Dare.LinkedListNode.prototype.getPrev = function () {
  return this.prev;
};
Dare.LinkedListNode.prototype.setPrev = function (node) {
  this.prev = node;
};
Dare.LinkedListNode.prototype.getNext = function () {
  return this.prev;
};
Dare.LinkedListNode.prototype.setNext = function (node) {
  this.prev = node;
};

linkedlist.js 链表类

/*
* 双向链表
*/
Dare.LinkedList = function () {
  this.head = null;
  this.current = null;
  this.tail = null;
  this.length = 0;
};
Dare.extend(Dare.LinkedList, Dare);
/*
* 尾插法添加节点
*/
Dare.LinkedList.prototype.appendNode = function (node) {
  if (this == null) return;
  if (node == null) return;
  var tail = this.tail;
  if (tail == null) {
    this.tail = this.head = node;
  }
  else {
    tail.next = node;
    node.prev = tail;
    this.tail = node;
  }
  this.length++;
};
/*
* 删除节点
*/
Dare.LinkedList.prototype.moveNode = function (node) {
  if (this == null) return;
  if (node == null) return;
  //中间节点
  var prev = node.prev;
  if (prev != null) {
    prev.next = node.next;
  }
  if (node.next != null) {
    node.next.prev = prev;
  }
  //头节点
  if (node == this.head) {
    this.head = node.next;
  }
  //尾节点
  if (node == this.tail) {
    if (prev != null) {
      this.tail = prev;
    }
    else {
      this.head = this.tail;
    }
  }
  node.prev = null;
  node.next = null;
  this.length--;
};
/*
* 构造节点
*/
Dare.LinkedList.prototype.constructNode = function (node, obj) {
  if (node == null || obj == null) return;
  node.data = obj;
  return node;
};
/*
* 获取节点数据
*/
Dare.LinkedList.prototype.getNodeData = function (node) {
  if (node == null) return;
  return node.data;
};
/*
* 从头开始
*/
Dare.LinkedList.prototype.start = function () {
  if (this == null) return;
  return this.current = this.head;
};
/*
* 从尾开始
*/
Dare.LinkedList.prototype.end = function () {
  if (this == null) return;
  return this.current = this.tail;
};
/*
* 下个节点
*/
Dare.LinkedList.prototype.nextNode = function () {
  if (this == null) return;
  if (this.current == null) return
  var node = this.current;
  this.current = this.current.next;
  return node;
};
/*
* 上个节点
*/
Dare.LinkedList.prototype.prevNode = function () {
  if (this == null) return;
  if (this.current == null) return
  var node = this.current;
  this.current = this.current.prev;
  return node;
};
/*
* 链表是否空
*/
Dare.LinkedList.prototype.isEmpty = function () {
  if (this == null) return true;
  if (this.head == null) {
    return true;
  }
  else {
    return false;
  }
};
/*
* 链表长度
*/
Dare.LinkedList.prototype.getLength = function () {
  if (this == null) return;
  return this.length;
};
/*
* 清空链表
*/
Dare.LinkedList.prototype.clearList = function () {
  this.head.next = null;
  this.head = null;
};
/*
* 是否存在节点
*/
Dare.LinkedList.prototype.containsNode = function (obj) {
  if (this == null) return false;
  var node = list.head;
  if (node == null) return false;
  while (node != null) {
    if (node.data == obj) {
      return true;
    }
    node = node.next;
  }
};

实战调用用例代码陆续更新:

<script type="text/javascript">
    var linkedList = new Dare.LinkedList();
    function createList() {
      for (var i = 0; i < 7; i++) {
        var movie = {};
        var linkedListNode = new Dare.LinkedListNode();

movie.id = i;
        movie.name = 'movie_' + i;
        linkedListNode.data = movie;
        linkedList.appendNode(linkedListNode); //创建链表
      }
      //deleteNode(linkedList);//删除节点
      //printList(linkedList); //输出链表
      printNode(linkedList);
    }
    function printList(list) {
      var node = list.head;
      if (node == null) return;
      var html = '';
      while (node != null) {
        var movie = node.data;
        html += movie.id + "|" + movie.name + "<br>";
        node = node.next;
      }
      document.write(html);
    }
    function deleteNode(list) {
      var node = list.head;
      if (node == null) return;
      var i = 0;
      while (node != null) {
        if (i == 3) {
          linkedList.moveNode(node); //删除指定节点
          break;
        }
        i++;
        node = node.next;
      }
    }
    var printNode = function(list) {
      var node = list.head;
      if (node == null) return;
      var i = 0;
      while (node != null) {
        if (i == 4) {
          var movie = linkedList.getNodeData(node); //打印指定节点
          document.writeln(movie.id + "<br>");
          document.writeln(movie.name + "<br>");
          break;
        }
        i++;
        node = node.next;
      }
    }
  </script>

转载于:https://www.cnblogs.com/fx2008/archive/2011/10/27/2226911.html

js实现双向链表互联网机顶盒实战应用相关推荐

  1. js面向对象开发互联网机顶盒应用头端之四

    Dare.InitSetting = function () {   this.className = 'Dare.InitSetting';   this.stylePath = '../style ...

  2. js面向对象开发互联网机顶盒应用头端之二

    /** * Dare Movie Object. * @constructor */ //声明构造函数 构造函数初始化变量 Dare.Movie = function() {   this.paren ...

  3. js面向对象开发互联网机顶盒应用头端之六

    /** * @function MediaMode 媒体模式 */ var MediaMode = {   NONE: -1, //无   MOVIE: 0, //电影   MUSIC: 1, //音 ...

  4. 互联网金融产品实战——安全开发篇

    说到安全开发,总绕不开两个词:OWASP(Open Web Application Security Project)和CWE(Common Weakness Enumeration),针对其中的某些 ...

  5. html+css+js适合前端小白的实战全解(超详细)——2048小游戏(三)

    续上一小节,我们回到newgame()这个函数,我们之前只做了init()内函数,相当于一个初始化操作 现在,我们需要再随机两个两个生成数字. 随机生成数字在这个游戏里会经常出现,用户移动一步,也会产 ...

  6. html+css+js适合前端小白的实战全解(超详细)——2048小游戏(二)

    续上一小节,我们可以发现每一个grid-cell上的数字初始时并不在格子里. 这些数字可以随着玩家的操作而移动 ​ 我们可以想象:初始时每一个格子上的数为0,他们并不显示 ↓ 只有当grid-cell ...

  7. Node.js CQRS 幻灯片系统开发实战-曾亮-专题视频课程

    Node.js CQRS 幻灯片系统开发实战-328人已学习 课程介绍         通过 Node.js . Express 和 CQRS 技术开发幻灯片系统. 我们已经习惯于 MVC 开发,也都 ...

  8. 《Vue.js 3移动应用开发实战》简介

    #好书推荐##好书奇遇季#<Vue.js 3移动应用开发实战>,京东当当天猫都有发售.定价79元,网店打折销售更便宜.本书内容系统全面,配套示例源码与PPT课件. 随着前后端分离开发模式的 ...

  9. 前端《Vue.js从入门到项目实战》PDF课件+《微信小程序实战入门第2版》PDF代码调试

    JS进行开发,正如一切的编程语言都立足于电元信号的正负极,即01码,可为什么软件都不采用二进制编码来 进行开发呢?这里面牵扯到一个成本的问题,这正是影响项目领导者进行决策的关键因素.Vue项目与原生J ...

最新文章

  1. 什么原因导致芯片短路_华为为什么突然大量用起了联发科芯片,或是这三个产品策略原因...
  2. 菜鸟教程html图片自动播放,HTML img 标签 | 菜鸟教程
  3. prim算法适用条件_内部排序算法的比较及应用
  4. 如何成为一个优秀的测试工程师?必备的9大职业素质盘点
  5. iText 实践的目录(the content of iText in Action)
  6. 机房服务器巡视项目,年底,机房巡检不能少
  7. gambit多面整合
  8. Python 之pandas库的安装及库安装两方法总结
  9. Eclipse的Debug调试技巧大全
  10. unity漂移 unity3d教程 // WheelCollider
  11. 【RDMA】RDMA编程实例(IBV Verbs )
  12. 如何通过自媒体创业月入万元
  13. via/route blockage/size blockage/wire/pin guide/pin blockage/partition
  14. 盒子模型(标准盒模型、怪异盒模型)
  15. excel 查找/替换 回车键
  16. soj 3172 Fisherman (01背包的装满)
  17. 呃呃呃呃呃呃鹅鹅鹅鹅鹅鹅饿
  18. E: 无法下载 http://us.archive.ubuntu.com/ubuntu/dists/xenial/main/binary-armhf/Packages 404 Not Found
  19. 淘宝新店引流方法在这里!
  20. 狗蛋与babel的初遭遇

热门文章

  1. C语言经典例11-斐波那契数列
  2. 【五线谱】踏板标记 ( 踩下踏板 Ped 标记 | 松开踏板 * 标记 | MIDI 中的对应踏板指令 | 连续控制信号 | 开关控制信号 )
  3. 【C 语言】文件操作 ( 配置文件读写 | 框架搭建 | 头文件定义 | 头文件导入限制 | 兼容 C++ 语言 | 函数形参输入输出属性注释)
  4. 【Android 逆向】Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | Visual Studio 中 SDK 和 NDK 安装位置 )
  5. 【Flutter】开发 Flutter 包和插件 ( 开发 Dart 插件包 | 发布 Dart 插件包 )
  6. 【Android APT】编译时技术 ( ButterKnife 原理分析 )
  7. 【Android 电量优化】电量优化 ( Battery Historian 环境要求 | 电量分析报告 | 电量优化三原则 | 电量优化注意事项 )
  8. 【Netty】Netty 简介 ( 原生 NIO 弊端 | Netty 框架 | Netty 版本 | 线程模型 | 线程 阻塞 IO 模型 | Reactor 模式引入 )
  9. 【Java 网络编程】TCP 数据传输示例 ( 客户端参数设置 | 服务器端参数设置 | ByteBuffer 存放读取数据类型 )
  10. 逃亡的准备(大数据版)