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 相关文章推荐
CSS常用网站布局实例
Apr 03 Javascript
parseInt parseFloat js字符串转换数字
Aug 01 Javascript
基本jquery的控制tabs打开的数量的代码
Oct 17 Javascript
Js-$.extend扩展方法使方法参数更灵活
Jan 15 Javascript
通过Tabs方法基于easyUI+bootstrap制作工作站
Mar 28 Javascript
BootStrap的alert提示框的关闭后再显示怎么解决
May 17 Javascript
浅析js的模块化编写 require.js
Dec 07 Javascript
Node.js学习之地址解析模块URL的使用详解
Sep 28 Javascript
vue+webpack模拟后台数据的示例代码
Jul 26 Javascript
JavaScript如何实现元素全排列实例代码
May 14 Javascript
Vue快速实现通用表单验证功能
Dec 05 Javascript
vue实现井字棋游戏
Sep 29 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
一些花式咖啡的配方
2021/03/03 冲泡冲煮
浅析is_writable的php实现
2013/06/18 PHP
微信公众号判断用户是否已关注php代码解析
2016/06/24 PHP
PHP实现限制IP访问及提交次数的方法详解
2017/07/17 PHP
PHP正则之正向预查与反向预查讲解与实例
2020/04/06 PHP
制作特殊字的脚本
2006/06/26 Javascript
表格单元格交错着色实现思路及代码
2013/04/01 Javascript
通过length属性判断jquery对象是否存在
2013/10/18 Javascript
js实现顶部可折叠的菜单工具栏效果实例
2015/05/09 Javascript
一不小心就做错的JS闭包面试题
2015/11/25 Javascript
在Linux系统中搭建Node.js开发环境的简单步骤讲解
2016/01/26 Javascript
javascript实现tab响应式切换特效
2016/01/29 Javascript
js实现表单及时验证功能 用户信息立即验证
2016/09/13 Javascript
详解使用JS如何制作简单的ASCII图与单极图
2017/03/31 Javascript
jQuery+HTML5实现WebGL高性能烟花绽放动画效果【附demo源码下载】
2017/08/18 jQuery
详解各版本React路由的跳转的方法
2018/05/10 Javascript
vue中的数据绑定原理的实现
2018/07/02 Javascript
vue实现类似淘宝商品评价页面星级评价及上传多张图片功能
2018/10/29 Javascript
vue实现带复选框的树形菜单
2019/05/27 Javascript
一看就会的vuex实现登录验证(附案例)
2020/01/09 Javascript
jquery实现直播视频弹幕效果
2020/02/25 jQuery
vue项目启动出现cannot GET /服务错误的解决方法
2020/04/26 Javascript
js实现简单的轮播图效果
2020/12/13 Javascript
python获取外网ip地址的方法总结
2015/07/02 Python
python实现文件路径和url相互转换的方法
2015/07/06 Python
python3+PyQt5+Qt Designer实现扩展对话框
2018/04/20 Python
python调用OpenCV实现人脸识别功能
2018/05/25 Python
使用keras内置的模型进行图片预测实例
2020/06/17 Python
html5 跨文档消息传输示例探讨
2013/04/01 HTML / CSS
大学生毕业自我鉴定范文
2013/09/19 职场文书
大学毕业生自荐书怎么写?
2014/01/06 职场文书
中职生自荐信范文
2014/06/15 职场文书
材料物理专业求职信
2014/09/01 职场文书
2014副局长群众路线对照检查材料思想汇报
2014/09/22 职场文书
钓鱼岛事件感想
2015/08/11 职场文书
Netty客户端接入流程NioSocketChannel创建解析
2022/03/25 Java/Android