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 页面自动加载函数(兼容多浏览器)
May 18 Javascript
javascript 得到文件后缀名的思路及实现
May 09 Javascript
Bootstrap轮播加上css3动画,炫酷到底!
Dec 22 Javascript
非常棒的jQuery图片轮播效果
Apr 17 Javascript
设置jQueryUI DatePicker默认语言为中文
Jun 04 Javascript
详谈for循环里面的break和continue语句
Jul 20 Javascript
javascript中toFixed()四舍五入使用方法详解
Sep 28 Javascript
记一次用vue做的活动页的方法步骤
Apr 11 Javascript
详解json串反转义(消除反斜杠)
Aug 12 Javascript
JS面试题中深拷贝的实现讲解
May 07 Javascript
vuex 多模块时 模块内部的mutation和action的调用方式
Jul 24 Javascript
如何用Node.js编写内存效率高的应用程序
Apr 30 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中的内存管理,PHP动态分配和释放内存
2013/06/28 PHP
PHP大文件切割上传并带进度条功能示例
2019/07/01 PHP
javascript addBookmark 加入收藏 多浏览器兼容
2009/08/15 Javascript
textarea中的手动换行处理的jquery代码
2011/02/26 Javascript
JQuery.ajax传递中文参数的解决方法 推荐
2011/03/28 Javascript
仿微博字符限制效果实现代码
2012/04/20 Javascript
Ajax局部更新导致JS事件重复触发问题的解决方法
2014/10/14 Javascript
js+jquery常用知识点汇总
2015/03/03 Javascript
Three.js学习之Lamber材质和Phong材质
2016/08/04 Javascript
微信小程序购物商城系统开发系列-目录结构介绍
2016/11/21 Javascript
bootstrap table 多选框分页保留示例代码
2017/03/08 Javascript
Vue2.0 从零开始_环境搭建操作步骤
2017/06/14 Javascript
Windows下快速搭建NodeJS本地服务器的步骤
2017/08/09 NodeJs
Vuejs 2.0 子组件访问/调用父组件的方法(示例代码)
2018/02/08 Javascript
jQuery实现的监听导航滚动置顶状态功能示例
2018/07/23 jQuery
vue展示dicom文件医疗系统的实现代码
2018/08/27 Javascript
手把手教你写一个微信小程序(推荐)
2018/10/17 Javascript
webpack4.0+vue2.0利用批处理生成前端单页或多页应用的方法
2019/06/28 Javascript
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
python用quad、dblquad实现一维二维积分的实例详解
2019/11/20 Python
对python中assert、isinstance的用法详解
2019/11/27 Python
Django 如何使用日期时间选择器规范用户的时间输入示例代码详解
2020/05/22 Python
利用scikitlearn画ROC曲线实例
2020/07/02 Python
html5实现滑块功能之type=&quot;range&quot;属性
2020/02/18 HTML / CSS
斯洛伐克时尚服装网上商店:Cellbes
2016/10/20 全球购物
社团文化节邀请函
2014/01/10 职场文书
物业保安员岗位职责制度
2014/01/30 职场文书
《走一步再走一步》教学反思
2014/02/15 职场文书
中国入世承诺
2014/04/01 职场文书
公司承诺函范文
2015/01/21 职场文书
教师个人总结范文
2015/02/11 职场文书
夏洛特的网观后感
2015/06/15 职场文书
青年文明号创建口号大全
2015/12/25 职场文书
实习报告怎么写
2019/06/20 职场文书
mysql字符串截取函数小结
2021/04/05 MySQL
MySQL七种JOIN类型小结
2021/10/24 MySQL