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 相关文章推荐
jQuery 源代码显示控件 (Ajax加载方式).
May 18 Javascript
禁止JQuery中的load方法装载IE缓存中文件的方法
Sep 11 Javascript
Js 刷新框架页的代码
Apr 13 Javascript
js实现文本框中输入文字页面中div层同步获取文本框内容的方法
Mar 03 Javascript
jQuery实现的网页左侧在线客服效果代码
Oct 23 Javascript
基于Particles.js制作超炫粒子动态背景效果(仿知乎)
Sep 13 Javascript
详解Angular路由之路由守卫
May 10 Javascript
详解Vue开发微信H5微信分享签名失败问题解决方案
Aug 09 Javascript
layer弹出子iframe层父子页面传值的实现方法
Nov 22 Javascript
JavaScript实现连连看连线算法
Jan 05 Javascript
使用jQuery实现掷骰子游戏
Oct 24 jQuery
一文彻底理解js原生语法prototype,__proto__和constructor
Oct 24 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/01 无线电
php验证session无效的解决方法
2014/11/04 PHP
PHP学习笔记(一):基本语法之标记、空白、和注释
2015/04/17 PHP
ThinkPHP安装和设置
2015/07/27 PHP
PHP几个实用自定义函数小结
2016/01/25 PHP
PHP回调函数与匿名函数实例详解
2017/08/16 PHP
理解Javascript_01_理解内存分配原理分析
2010/10/11 Javascript
探讨JavaScript中声明全局变量三种方式的异同
2013/12/03 Javascript
js 跳出页面的frameset框架示例介绍
2013/12/23 Javascript
jquery解析XML字符串和XML文件的方法说明
2014/02/21 Javascript
JavaScript实现下拉列表框数据增加、删除、上下排序的方法
2015/08/11 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
原生Javascript插件开发实践
2017/01/18 Javascript
JS实现含有中文字符串的友好截取功能分析
2017/03/13 Javascript
微信小程序之购物车功能
2020/09/23 Javascript
Vue2.0实现购物车功能
2017/06/05 Javascript
javascript+jQuery实现360开机时间显示效果
2017/11/03 jQuery
Vue2.0 事件的广播与接收(观察者模式)
2018/03/14 Javascript
微信小程序按钮去除边框线分享页面功能
2018/08/27 Javascript
使用vue开发移动端管理后台的注意事项
2019/03/07 Javascript
世界上最短的数字判断js代码
2019/09/09 Javascript
简单了解常用的JavaScript 库
2020/07/16 Javascript
python使用os模块的os.walk遍历文件夹示例
2014/01/27 Python
python调用java模块SmartXLS和jpype修改excel文件的方法
2015/04/28 Python
python字典一键多值实例代码分享
2019/06/14 Python
Python实现中值滤波去噪方式
2019/12/18 Python
Python3操作读写CSV文件使用包过程解析
2020/04/10 Python
Python连接HDFS实现文件上传下载及Pandas转换文本文件到CSV操作
2020/06/06 Python
python与pycharm有何区别
2020/07/01 Python
什么是数组名
2012/05/10 面试题
2015年学雷锋活动总结
2015/02/06 职场文书
商务司机岗位职责
2015/04/10 职场文书
2015年个人工作总结报告
2015/04/25 职场文书
温馨祝福晨语:美丽的一天从我的问候开始
2019/11/28 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
详解vue中v-for的key唯一性
2021/05/15 Vue.js