js實現(xiàn)雙向鏈表互聯(lián)網(wǎng)機頂盒實戰(zhàn)應(yīng)用實現(xiàn)
更新時間:2011年10月28日 23:51:40 作者:
js實現(xiàn)雙向鏈表互聯(lián)網(wǎng)機頂盒實戰(zhàn)應(yīng)用實現(xiàn),需要的朋友可以參考下。
上實戰(zhàn)代碼:
linkedlistnode.js 節(jié)點類
/*
* 鏈表節(jié)點
*/
Dare.LinkedListNode = function () {
this.data = null;//數(shù)據(jù)域
this.prev = null;//前驅(qū)
this.next = null;//后驅(qū)
};
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);
/*
* 尾插法添加節(jié)點
*/
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++;
};
/*
* 刪除節(jié)點
*/
Dare.LinkedList.prototype.moveNode = function (node) {
if (this == null) return;
if (node == null) return;
//中間節(jié)點
var prev = node.prev;
if (prev != null) {
prev.next = node.next;
}
if (node.next != null) {
node.next.prev = prev;
}
//頭節(jié)點
if (node == this.head) {
this.head = node.next;
}
//尾節(jié)點
if (node == this.tail) {
if (prev != null) {
this.tail = prev;
}
else {
this.head = this.tail;
}
}
node.prev = null;
node.next = null;
this.length--;
};
/*
* 構(gòu)造節(jié)點
*/
Dare.LinkedList.prototype.constructNode = function (node, obj) {
if (node == null || obj == null) return;
node.data = obj;
return node;
};
/*
* 獲取節(jié)點數(shù)據(jù)
*/
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;
};
/*
* 下個節(jié)點
*/
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;
};
/*
* 上個節(jié)點
*/
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;
};
/*
* 是否存在節(jié)點
*/
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;
}
};
實戰(zhàn)調(diào)用用例代碼陸續(xù)更新:
<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); //創(chuàng)建鏈表
}
//deleteNode(linkedList);//刪除節(jié)點
//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); //刪除指定節(jié)點
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); //打印指定節(jié)點
document.writeln(movie.id + "<br>");
document.writeln(movie.name + "<br>");
break;
}
i++;
node = node.next;
}
}
</script>
linkedlistnode.js 節(jié)點類
復(fù)制代碼 代碼如下:
/*
* 鏈表節(jié)點
*/
Dare.LinkedListNode = function () {
this.data = null;//數(shù)據(jù)域
this.prev = null;//前驅(qū)
this.next = null;//后驅(qū)
};
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 鏈表類
復(fù)制代碼 代碼如下:
/*
* 雙向鏈表
*/
Dare.LinkedList = function () {
this.head = null;
this.current = null;
this.tail = null;
this.length = 0;
};
Dare.extend(Dare.LinkedList, Dare);
/*
* 尾插法添加節(jié)點
*/
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++;
};
/*
* 刪除節(jié)點
*/
Dare.LinkedList.prototype.moveNode = function (node) {
if (this == null) return;
if (node == null) return;
//中間節(jié)點
var prev = node.prev;
if (prev != null) {
prev.next = node.next;
}
if (node.next != null) {
node.next.prev = prev;
}
//頭節(jié)點
if (node == this.head) {
this.head = node.next;
}
//尾節(jié)點
if (node == this.tail) {
if (prev != null) {
this.tail = prev;
}
else {
this.head = this.tail;
}
}
node.prev = null;
node.next = null;
this.length--;
};
/*
* 構(gòu)造節(jié)點
*/
Dare.LinkedList.prototype.constructNode = function (node, obj) {
if (node == null || obj == null) return;
node.data = obj;
return node;
};
/*
* 獲取節(jié)點數(shù)據(jù)
*/
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;
};
/*
* 下個節(jié)點
*/
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;
};
/*
* 上個節(jié)點
*/
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;
};
/*
* 是否存在節(jié)點
*/
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;
}
};
實戰(zhàn)調(diào)用用例代碼陸續(xù)更新:
復(fù)制代碼 代碼如下:
<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); //創(chuàng)建鏈表
}
//deleteNode(linkedList);//刪除節(jié)點
//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); //刪除指定節(jié)點
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); //打印指定節(jié)點
document.writeln(movie.id + "<br>");
document.writeln(movie.name + "<br>");
break;
}
i++;
node = node.next;
}
}
</script>
相關(guān)文章
一直都需要的復(fù)制到系統(tǒng)剪貼板之IE,firefox兼容版
一直都需要的復(fù)制到系統(tǒng)剪貼板之IE,firefox兼容版...2007-09-09基于jquery實現(xiàn)導(dǎo)航菜單高亮顯示(兩種方法)
本篇文章是基于jquery實現(xiàn)導(dǎo)航菜單高亮顯示,當點擊不同導(dǎo)航菜單實現(xiàn)當前點擊的菜單是高亮的,有需要的朋友可以關(guān)注下本文2015-08-08uniapp地圖組件(map)使用與遇到的一些問題總結(jié)
uniapp是用vue.js開發(fā)所有前端應(yīng)用的框架,開發(fā)人員只需要編寫一套代碼就可以發(fā)布到安卓、iOS、H5和小程序、快應(yīng)用等平臺,下面這篇文章主要給大家介紹了關(guān)于uniapp地圖組件(map)使用與遇到的一些問題,需要的朋友可以參考下2022-07-07javascript實現(xiàn)PC網(wǎng)頁里的拖拽效果
這篇文章主要介紹了javascript實現(xiàn)PC網(wǎng)頁里的拖拽效果的相關(guān)資料,需要的朋友可以參考下2016-03-03詳解JS構(gòu)造函數(shù)中this和return
本文通過實例代碼給大家介紹了JS構(gòu)造函數(shù)中this和return,需要的朋友參考下吧2017-09-09關(guān)于AOP在JS中的實現(xiàn)與應(yīng)用詳解
這篇文章主要給大家介紹了關(guān)于AOP在JS中的實現(xiàn)與應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用JS具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-05-05