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


Posted in Javascript onOctober 28, 2011

上实战代码:
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>
Javascript 相关文章推荐
Jquery插件easyUi表单验证提交(示例代码)
Dec 30 Javascript
Extjs Label的 fieldLabel和html属性值对齐的方法
Jun 15 Javascript
jQuery异步上传文件插件ajaxFileUpload详细介绍
May 19 Javascript
JavaScript判断微信浏览器实例代码
Jun 13 Javascript
js定时器+简单的动画效果实例
Nov 10 Javascript
基于vue2.x的电商图片放大镜插件的使用
Jan 22 Javascript
jQuery niceScroll滚动条错位问题的解决方法
Feb 03 jQuery
解决修复npm安装全局模块权限的问题
May 17 Javascript
WEEX环境搭建与入门详解
Oct 16 Javascript
webgl实现物体描边效果的方法介绍
Nov 27 Javascript
解决vue watch数据的方法被调用了两次的问题
Nov 07 Javascript
超详细小程序定位地图模块全系列开发教学
Nov 24 Javascript
js常用代码段收集
Oct 28 #Javascript
jWiard 基于JQuery的强大的向导控件介绍
Oct 28 #Javascript
理解JSON:3分钟课程
Oct 28 #Javascript
Kibo 用于处理键盘事件的Javascript工具库
Oct 28 #Javascript
stream.js 一个很小、完全独立的Javascript类库
Oct 28 #Javascript
能说明你的Javascript技术很烂的五个原因分析
Oct 28 #Javascript
基于jquery的滚动鼠标放大缩小图片效果
Oct 27 #Javascript
You might like
rrmdir php中递归删除目录及目录下的文件
2011/05/15 PHP
php数组函数序列之array_push() 数组尾部添加一个或多个元素(入栈),返回新长度。
2011/11/07 PHP
PHP 自定义错误处理函数的使用详解
2013/05/10 PHP
div li的多行多列 无刷新分页示例代码
2013/10/16 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
用PHP生成excel文件到指定目录
2015/06/22 PHP
PHP json_encode() 函数详解及中文乱码问题
2015/11/05 PHP
Symfony2 session用法实例分析
2016/02/04 PHP
JavaScript获取图片真实大小代码实例
2014/09/24 Javascript
JavaScript中exec函数用法实例分析
2015/06/08 Javascript
举例讲解如何判断JavaScript中对象的类型
2016/04/22 Javascript
javascript时间差插件分享
2016/07/18 Javascript
原生js编写焦点图效果
2016/12/08 Javascript
jQuery animate()实现背景色渐变效果的处理方法【使用jQuery.color.js插件】
2017/03/15 Javascript
了解VUE的render函数的使用
2017/06/08 Javascript
vscode中vue-cli项目es-lint的配置方法
2018/07/30 Javascript
vue+springboot+element+vue-resource实现文件上传教程
2020/10/21 Javascript
详解js创建对象的几种方式和对象方法
2021/03/01 Javascript
python基础教程之lambda表达式使用方法
2014/02/12 Python
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
2017/03/20 Python
Python Requests库基本用法示例
2018/08/20 Python
Python元组知识点总结
2019/02/18 Python
Python3.5 + sklearn利用SVM自动识别字母验证码方法示例
2019/05/10 Python
Selenium+BeautifulSoup+json获取Script标签内的json数据
2020/12/07 Python
如何查看python关键字
2021/01/17 Python
美国医生配方营养补充剂供应商:Healthy Directions
2019/07/10 全球购物
error和exception有什么区别
2012/10/02 面试题
资产经营总监岗位职责
2013/12/04 职场文书
中学生英语演讲稿
2014/04/26 职场文书
教研处工作方案
2014/05/26 职场文书
关于运动会的口号
2014/06/07 职场文书
党员干部学习十八届五中全会精神心得体会
2016/01/05 职场文书
PHP实现rar解压读取扩展包小结
2021/06/03 PHP
python中的装饰器该如何使用
2021/06/18 Python
浅谈resultMap的用法及关联结果集映射
2021/06/30 Java/Android
《LOL》“克隆大作战”久违归来 幻灵战队皮肤上线
2022/04/03 其他游戏