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精华代码集
Jan 24 Javascript
Javascript中的数学函数
Apr 04 Javascript
JavaScript中的new的使用方法与注意事项
May 16 Javascript
jquery设置控件位置的方法
Aug 21 Javascript
jquery ajax的success回调函数中实现按钮置灰倒计时
Nov 19 Javascript
javascript操作css属性
Dec 30 Javascript
jquery控制背景音乐开关与自动播放提示音的方法
Feb 06 Javascript
Ionic如何实现下拉刷新与上拉加载功能
Jun 03 Javascript
easyui tree带checkbox实现单选的简单实例
Nov 07 Javascript
JS实现访问DOM对象指定节点的方法示例
Apr 04 Javascript
详解JavaScript的BUG和错误
May 07 Javascript
Vuejs监听vuex中值的变化的方法示例
Dec 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模板之Phpbean的目录结构
2008/01/10 PHP
解决file_get_contents无法请求https连接的方法
2013/12/17 PHP
Thinkphp无限级分类代码
2015/11/11 PHP
php+redis实现商城秒杀功能
2020/11/19 PHP
HTML代码中标签的全部属性 中文注释说明
2009/03/26 Javascript
JavaScript游戏之优化篇
2010/11/08 Javascript
javascript 学习笔记(八)javascript对象
2011/04/12 Javascript
JavaScript控制各种浏览器全屏模式的方法、属性和事件介绍
2014/04/03 Javascript
JavaScript使用循环和分割来替换和删除元素实例
2014/10/13 Javascript
jQuery插件slider实现拖动滑块选取价格范围
2015/04/30 Javascript
node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法
2016/12/18 Javascript
JS实现中文汉字按拼音排序的方法
2017/10/09 Javascript
VeeValidate在vue项目里表单校验应用案例
2018/05/09 Javascript
vue项目首屏加载时间优化实战
2019/04/23 Javascript
小程序rich-text组件如何改变内部img图片样式的方法
2019/05/22 Javascript
countUp.js实现数字滚动效果
2019/10/18 Javascript
JavaScript鼠标悬停事件用法解析
2020/05/15 Javascript
[03:17]DOTA2英雄基础教程 剧毒术士
2013/12/12 DOTA
python3中dict(字典)的使用方法示例
2017/03/22 Python
python利用dir函数查看类中所有成员函数示例代码
2017/09/08 Python
Python通过Pygame绘制移动的矩形实例代码
2018/01/03 Python
在ubuntu16.04中将python3设置为默认的命令写法
2018/10/31 Python
余弦相似性计算及python代码实现过程解析
2019/09/18 Python
python基于FTP实现文件传输相关功能代码实例
2019/09/28 Python
Python Matplotlib简易教程(小白教程)
2020/07/28 Python
html5唤醒APP小记
2019/03/27 HTML / CSS
SQL Server面试题
2013/04/04 面试题
体育专业个人求职信范文
2013/12/27 职场文书
《美丽的公鸡》教学反思
2014/02/25 职场文书
元旦寄语大全
2014/04/10 职场文书
《郑和远航》教学反思
2014/04/16 职场文书
党支部综合考察材料
2014/05/19 职场文书
化工见习报告范文
2014/10/31 职场文书
教师思想工作总结2015
2015/05/13 职场文书
Elasticsearch Recovery 详细介绍
2022/04/19 Java/Android
MySQL去除密码登录告警的方法
2022/04/20 MySQL