js实现双向链表互联网机顶盒实战应用实现

 更新时间:2011年10月28日 23:51:40   作者:  
js实现双向链表互联网机顶盒实战应用实现,需要的朋友可以参考下。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

上实战代码:
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>

相关文章

  • 公众号SVG动画交互实战代码

    公众号SVG动画交互实战代码

    这篇文章主要介绍了公众号SVG动画交互实战代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • js获取新浪天气接口的实现代码

    js获取新浪天气接口的实现代码

    下面小编就为大家带来一篇js获取新浪天气接口的实现代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • js实现微信聊天效果

    js实现微信聊天效果

    这篇文章主要为大家详细介绍了js实现微信聊天效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • php简单数据库操作类的封装

    php简单数据库操作类的封装

    这篇文章主要为大家详细介绍了php简单数据库操作类的封装,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • js仿360开机效果

    js仿360开机效果

    这篇文章主要为大家详细介绍了js仿360开机效果,并且封装一个带回调函数的缓动动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • JavaScript模板引擎实现原理实例详解

    JavaScript模板引擎实现原理实例详解

    这篇文章主要介绍了JavaScript模板引擎实现原理,结合实例形式详细分析了JavaScript模板引擎原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2018-12-12
  • javascript插件开发的一些感想和心得

    javascript插件开发的一些感想和心得

    这篇文章主要介绍了javascript插件开发的一些感想和心得,需要的朋友可以参考下
    2016-02-02
  • JSON.parse处理非标准Json数据出错的解决

    JSON.parse处理非标准Json数据出错的解决

    这篇文章主要介绍了JSON.parse处理非标准Json数据出错的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • js中的escape及unescape函数的php实现代码

    js中的escape及unescape函数的php实现代码

    js中的escape及unescape函数的php实现代码...
    2007-09-09
  • JavaScript的Module模式编程深入分析

    JavaScript的Module模式编程深入分析

    Module模式是常见的JavaScript编程模式,一般来说这种模式是很好理解的,但是依然有一些高级的用法没有得到太多的注意。在这篇文章中我会提到Module模式的基础知识和一些真正重要的话题,包括一个可能是我原创的
    2013-08-08

最新评论

?


http://www.vxiaotou.com