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处理表格对table进行修饰
May 26 Javascript
利用jquery操作Radio方法小结
Oct 20 Javascript
JavaScript实现MIPS乘法模拟的方法
Apr 17 Javascript
jquery淡入淡出效果简单实例
Jan 14 Javascript
JS实现图片预加载之无序预加载功能代码
May 12 Javascript
JavaScript实现图片拖曳效果
Sep 08 Javascript
原生JS实现动态加载js文件并在加载成功后执行回调函数的方法
Dec 30 Javascript
vue中axios实现数据交互与跨域问题
May 12 Javascript
Vue使用Clipboard.JS在h5页面中复制内容实例详解
Sep 03 Javascript
js实现转动骰子模型
Oct 24 Javascript
微信小程序8种数据通信的方式小结
Feb 03 Javascript
微信小程序实现简单文字跑马灯
May 26 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
PHP4实际应用经验篇(8)
2006/10/09 PHP
php后门URL的防范
2013/11/12 PHP
php中的静态变量的基本用法
2014/03/20 PHP
PHP使用内置函数生成图片的方法详解
2016/05/09 PHP
Thinkphp实现短信验证注册功能
2016/10/18 PHP
jquery获取input的value问题说明
2010/08/19 Javascript
使用nodejs、Python写的一个简易HTTP静态文件服务器
2014/07/18 NodeJs
jQuery如何防止这种冒泡事件发生
2015/02/27 Javascript
Vue表单实例代码
2016/09/05 Javascript
HTML中setCapture、releaseCapture 使用方法浅析
2016/09/25 Javascript
JS实现表单验证功能(验证手机号是否存在,验证码倒计时)
2016/10/11 Javascript
通过vue-cli3构建一个SSR应用程序的方法
2018/09/13 Javascript
Vue 重置组件到初始状态的方法示例
2018/10/10 Javascript
微信小程序实现批量倒计时功能
2020/11/01 Javascript
layui将table转化表单显示的方法(即table.render转为表单展示)
2019/09/24 Javascript
vue vantUI实现文件(图片、文档、视频、音频)上传(多文件)
2019/10/15 Javascript
VUE 解决mode为history页面为空白的问题
2019/11/01 Javascript
Node.js中console.log()输出彩色字体的方法示例
2019/12/01 Javascript
[54:53]完美世界DOTA2联赛PWL S2 GXR vs PXG 第二场 11.18
2020/11/18 DOTA
Python中functools模块的常用函数解析
2016/06/30 Python
对Python3 goto 语句的使用方法详解
2019/02/16 Python
Pyqt5 基本界面组件之inputDialog的使用
2019/06/25 Python
python GUI图形化编程wxpython的使用
2019/07/19 Python
pytorch masked_fill报错的解决
2020/02/18 Python
Pandas的Apply函数具体使用
2020/07/21 Python
浅谈HTML5 defer和async的区别
2016/06/07 HTML / CSS
大学生求职中的自我评价
2013/10/01 职场文书
人力资源部经理岗位职责规定
2014/02/23 职场文书
大学生社会实践方案
2014/05/11 职场文书
搞笑车尾标语
2014/06/23 职场文书
中秋节国旗下演讲稿
2014/09/05 职场文书
2015年人事科工作总结
2015/04/28 职场文书
致运动员的广播稿
2015/08/19 职场文书
2016年度继续教育学习心得体会
2016/01/19 职场文书
Java 实现限流器处理Rest接口请求详解流程
2021/11/02 Java/Android
分享python函数常见关键字
2022/04/26 Python