js實現雙向鏈表互連網機頂盒實戰應用實現

來源:互聯網
上載者:User

上實戰代碼:
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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.