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 相关文章推荐
JavaScript arguments 多参传值函数
Oct 24 Javascript
Jquery节点遍历next与nextAll方法使用示例
Jul 22 Javascript
常用js,css文件统一加载方法(推荐) 并在加载之后调用回调函数
Sep 23 Javascript
URL中“#” “?” &amp;“”号的作用浅析
Feb 04 Javascript
mockjs,json-server一起搭建前端通用的数据模拟框架教程
Dec 18 Javascript
微信小程序局部刷新触发整页刷新效果的实现代码
Nov 21 Javascript
vscode下的vue文件格式化问题
Nov 28 Javascript
JavaScript高阶教程之“==”隐藏下的类型转换
Apr 11 Javascript
localstorage实现带过期时间的缓存功能
Jun 28 Javascript
js中调用微信的扫描二维码功能的实现代码
Apr 11 Javascript
vue 内联样式style中的background用法说明
Aug 05 Javascript
nuxt 路由、过渡特效、中间件的实现代码
Nov 06 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
5.PHP的其他功能
2006/10/09 PHP
在Laravel5中正确设置文件权限的方法
2019/05/22 PHP
对laravel的csrf 防御机制详解,及form中csrf_token()的存在介绍
2019/10/24 PHP
PHP copy函数使用案例代码解析
2020/09/01 PHP
javascript实现颜色渐变的方法
2013/10/30 Javascript
javascript中2个感叹号的用法实例详解
2014/09/04 Javascript
JavaScript组件开发完整示例
2015/12/15 Javascript
JS实现简单的二维矩阵乘积运算
2016/01/26 Javascript
jQuery动态创建元素以及追加节点的实现方法
2016/10/20 Javascript
从零学习node.js之简易的网络爬虫(四)
2017/02/22 Javascript
jQuery实现手机号正则验证输入及自动填充空格功能
2018/01/02 jQuery
利用js实现前后台传送Json的示例代码
2018/03/29 Javascript
JS与jQuery实现ListBox上移,下移,左移,右移操作功能示例
2018/05/31 jQuery
Angular-UI Bootstrap组件实现警报功能
2018/07/16 Javascript
nodejs分离html文件里面的js和css的方法
2019/04/09 NodeJs
three.js 如何制作魔方
2020/07/31 Javascript
node脚手架搭建服务器实现token验证的方法
2021/01/20 Javascript
浅谈使用Python变量时要避免的3个错误
2017/10/30 Python
通过python+selenium3实现浏览器刷简书文章阅读量
2017/12/26 Python
python web自制框架之接受url传递过来的参数实例
2018/12/17 Python
画pytorch模型图,以及参数计算的方法
2019/08/17 Python
使用 Python 清理收藏夹里已失效的网站
2019/12/03 Python
python函数enumerate,operator和Counter使用技巧实例小结
2020/02/22 Python
使用Django清空数据库并重新生成
2020/04/03 Python
python UIAutomator2使用超详细教程
2021/02/19 Python
美国家庭鞋店:Shoe Sensation
2019/09/27 全球购物
你所知道的集合类都有哪些?主要方法?
2012/12/31 面试题
岗位职责范本
2013/11/23 职场文书
大学自荐信
2013/12/12 职场文书
学校运动会开幕演讲稿
2014/01/04 职场文书
《夹竹桃》教学反思
2014/04/20 职场文书
优秀班组长事迹
2014/05/31 职场文书
党的群众路线教育实践活动心得体会(企业)
2014/11/03 职场文书
安全责任协议书范本
2016/03/23 职场文书
七年级作文之秋游
2019/10/21 职场文书
SpringCloud之@FeignClient()注解的使用方式
2021/09/25 Java/Android