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操作userdata
Apr 27 Javascript
input的focus方法使用
Mar 13 Javascript
JS 自定义函数缺省值的设置方法
May 05 Javascript
使用js写的一个简易的投票
Nov 27 Javascript
打造个性化的功能强大的Jquery虚拟键盘(VirtualKeyboard)
Oct 11 Javascript
jQuery DOM删除节点操作指南
Mar 03 Javascript
js格式化时间的简单实例
Nov 27 Javascript
利用策略模式与装饰模式扩展JavaScript表单验证功能
Feb 14 Javascript
node.js利用redis数据库缓存数据的方法
Mar 01 Javascript
老生常谈JavaScript面向对象基础与this指向问题
Oct 16 Javascript
layui-laydate时间日历控件使用方法详解
Nov 15 Javascript
antd Select下拉菜单动态添加option里的内容操作
Nov 02 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
十天学会php之第八天
2006/10/09 PHP
PHP中的函数嵌套层数限制分析
2011/06/13 PHP
关于zend studio 出现乱码问题的总结
2013/06/23 PHP
ThinkPHP利用PHPMailer实现邮件发送实现代码
2013/09/26 PHP
php根据身份证号码计算年龄的实例代码
2014/01/18 PHP
laravel通过创建自定义artisan make命令来新建类文件详解
2017/08/17 PHP
如何书写高质量jQuery代码(使用jquery性能问题)
2014/06/30 Javascript
jQuery通过控制节点实现仅在前台通过get方法完成参数传递
2015/02/02 Javascript
jQuery实现仿淘宝带有指示条的图片转动切换效果完整实例
2015/03/04 Javascript
jQuery实现表格行和列的动态添加与删除方法【测试可用】
2016/08/01 Javascript
解决Window10系统下Node安装报错的问题分析
2016/12/13 Javascript
layui弹出层效果实现代码
2017/05/19 Javascript
通过webpack引入第三方库的方法
2018/07/20 Javascript
vue+springboot实现项目的CORS跨域请求
2018/09/05 Javascript
详解Webpack抽离第三方类库以及common解决方案
2020/03/30 Javascript
js仿京东放大镜效果
2020/08/09 Javascript
VsCode里的Vue模板的实现
2020/08/12 Javascript
[01:19:33]DOTA2-DPC中国联赛 正赛 iG vs VG BO3 第一场 2月2日
2021/03/11 DOTA
python计数排序和基数排序算法实例
2014/04/25 Python
Python实例之wxpython中Frame使用方法
2014/06/09 Python
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
Python2.x中str与unicode相关问题的解决方法
2015/03/30 Python
Python lambda表达式用法实例分析
2018/12/25 Python
python重要函数eval多种用法解析
2020/01/14 Python
Python3基于print打印带颜色字符串
2020/07/06 Python
jupyter notebook远程访问不了的问题解决方法
2021/01/11 Python
利用HTML5中Geolocation获取地理位置调用Google Map API在Google Map上定位
2013/01/23 HTML / CSS
GAP阿联酋官网:GAP UAE
2017/11/30 全球购物
L’AGENCE官网:加州女装品牌
2018/06/03 全球购物
质检员的岗位职责
2013/11/15 职场文书
公司年会抽奖活动主持词
2014/03/31 职场文书
幼儿园辞职书
2015/02/26 职场文书
文明旅游倡议书
2015/04/28 职场文书
结婚典礼主持词
2015/06/29 职场文书
《百分数的认识》教学反思
2016/02/19 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书