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 组合form元素为json格式,asp.net反序列化
Jul 09 Javascript
动态载入/删除/更新外部 JavaScript/Css 文件的代码
Jul 03 Javascript
jqPlot 基于jquery的画图插件
Apr 26 Javascript
javascript中AJAX用法实例分析
Jan 30 Javascript
Node.js v8.0.0正式发布!看看带来了哪些主要新特性
Jun 02 Javascript
JS实现微信里判断页面是否被分享成功的方法
Jun 06 Javascript
AngularJS 实现点击按钮获取验证码功能实例代码
Jul 13 Javascript
JavaScript中使用参数个数实现重载功能
Sep 01 Javascript
使用vue-cli创建项目的图文教程(新手入门篇)
May 02 Javascript
关于AOP在JS中的实现与应用详解
May 06 Javascript
js通过循环多张图片实现动画效果
Dec 19 Javascript
深入了解Vue3模板编译原理
Nov 19 Vue.js
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
jquery将一个表单序列化为一个对象的方法
2013/12/02 Javascript
js分页代码分享
2014/04/28 Javascript
js简单实现交换Li的值
2014/05/22 Javascript
javascript中parseInt()函数的定义和用法分析
2014/12/20 Javascript
JavaScript函数的一些注意要点小结及js匿名函数
2015/11/10 Javascript
JavaScript 计算笛卡尔积实例详解
2016/12/02 Javascript
js 文字超出长度用省略号代替,鼠标悬停并以悬浮框显示实例
2016/12/06 Javascript
js 概率计算(简单版)
2017/09/12 Javascript
vue中动态设置meta标签和title标签的方法
2018/07/11 Javascript
vue路由--网站导航功能详解
2019/03/29 Javascript
微信小程序 确认框的实现(附代码)
2019/07/23 Javascript
JavaScript中this函数使用实例解析
2020/02/21 Javascript
微信小程序开发搜索功能实现(前端+后端+数据库)
2020/03/04 Javascript
JS实现鼠标按下拖拽效果
2020/07/23 Javascript
原生js实现贪吃蛇游戏
2020/10/26 Javascript
JavaScript实现筛选数组
2021/03/02 Javascript
在Python中使用dict和set方法的教程
2015/04/27 Python
pandas 条件搜索返回列表的方法
2018/10/30 Python
python+selenium实现自动抢票功能实例代码
2018/11/23 Python
一篇文章搞懂Python的类与对象名称空间
2018/12/10 Python
Python爬虫:url中带字典列表参数的编码转换方法
2019/08/21 Python
keras模型可视化,层可视化及kernel可视化实例
2020/01/24 Python
python 爬取疫情数据的源码
2020/02/09 Python
在pycharm中实现删除bookmark
2020/02/14 Python
html5构建触屏网站之网站尺寸探讨
2013/01/07 HTML / CSS
Petmate品牌官方网站:宠物用品
2018/11/25 全球购物
澳大利亚和新西兰最大的在线旅行社之一:Aunt Betty
2019/08/07 全球购物
《春天来了》教学反思
2014/04/07 职场文书
小学一年级评语大全
2014/04/22 职场文书
青春励志演讲稿范文
2014/08/25 职场文书
忠诚奉献演讲稿
2014/09/12 职场文书
2014年办公室文秘工作总结
2014/12/09 职场文书
天坛导游词
2015/02/02 职场文书
亲戚关系证明
2015/06/24 职场文书
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
2021/04/06 Oracle
mysql通过group by分组取最大时间对应数据的两种有效方法
2022/09/23 MySQL