您好,欢迎访问一九零五行业门户网

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

上实战代码:
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;
}
};
实战调用用例代码陆续更新:
复制代码 代码如下:
其它类似信息

推荐信息