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 相关文章推荐
JS无限树状列表实现代码
Jan 11 Javascript
IE6 fixed的完美解决方案
Mar 31 Javascript
JS随机调用指定函数的方法
Jul 01 Javascript
JS中的eval 为什么加括号
Apr 13 Javascript
jQuery 中ajax异步调用的四种方式
Jun 28 Javascript
值得分享的Bootstrap Table使用教程
Nov 23 Javascript
JavaScript之iterable_动力节点Java学院整理
Jun 29 Javascript
vue中路由验证和相应拦截的使用详解
Dec 13 Javascript
JavaScript 对引擎、运行时、调用堆栈的概述理解
Oct 22 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
Nov 28 Javascript
JS实现二维数组元素的排列组合运算简单示例
Jan 28 Javascript
JavaScript代理模式原理与用法实例详解
Mar 10 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
Flash空降上海 化身大魔王接受挑战
2020/03/02 星际争霸
Smarty foreach控制循环次数的一些方法
2015/07/01 PHP
PHP基于mcript扩展实现对称加密功能示例
2019/02/21 PHP
jquery getScript动态加载JS方法改进详解
2012/11/15 Javascript
不同的jQuery API来处理不同的浏览器事件
2012/12/09 Javascript
js post提交调用方法
2014/02/12 Javascript
JS模仿腾讯图片站的图片翻页按钮效果完整实例
2016/06/21 Javascript
Vue2.0组件间数据传递示例
2017/03/07 Javascript
angularjs定时任务的设置与清除示例
2017/06/02 Javascript
jquery+css实现侧边导航栏效果
2017/06/12 jQuery
基于jQuery选择器之表单对象属性筛选选择器的实例
2017/09/19 jQuery
jquery中有哪些api jQuery主要API
2017/11/20 jQuery
JavaScript实现猜数字游戏
2020/05/20 Javascript
实例分析javascript中的异步
2020/06/02 Javascript
用Python编写一个简单的俄罗斯方块游戏的教程
2015/04/03 Python
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
2017/08/08 Python
使用Template格式化Python字符串的方法
2019/01/22 Python
Python完成哈夫曼树编码过程及原理详解
2019/07/29 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
python的launcher用法知识点总结
2020/08/07 Python
CSS3动画:5种预载动画效果实例
2017/04/05 HTML / CSS
实例讲解HTML5的meta标签的一些应用
2015/12/08 HTML / CSS
美国一家运动专业鞋类零售商:Warehouse Shoe Sale(WSS)
2018/03/28 全球购物
SQL面试题
2013/04/30 面试题
工程项目建议书范文
2014/03/12 职场文书
小学三八妇女节活动方案
2014/03/16 职场文书
《忆江南》教学反思
2014/04/07 职场文书
综治宣传月活动总结
2014/04/28 职场文书
环境保护与污染治理求职信
2014/07/16 职场文书
后进基层党组织整改方案
2014/10/25 职场文书
音乐教师个人总结
2015/02/06 职场文书
2015年清明节网上祭英烈留言寄语
2015/03/04 职场文书
客户付款通知书
2015/04/23 职场文书
2015年教师国培感言
2015/08/01 职场文书
幼儿园安全教育随笔
2015/08/14 职场文书
pytorch--之halfTensor的使用详解
2021/05/24 Python