Js implementation of a two-way linked list Internet set-top box practical application

Source: Internet
Author: User

Actual code:
Linkedlistnode. js node class Copy codeThe Code is as follows :/*
* Linked list Node
*/
Dare. inclulistnode = function (){
This. data = null; // data field
This. prev = null; // Precursor
This. next = null; // post drive
};
Dare. extend (Dare. inclulistnode, Dare );
Dare. inclulistnode. prototype. getValue = function (){
Return this. data;
};
Dare. inclulistnode. prototype. setValue = function (obj ){
This. data = obj;
};
Dare. inclulistnode. prototype. getPrev = function (){
Return this. prev;
};
Dare. inclulistnode. prototype. setPrev = function (node ){
This. prev = node;
};
Dare. inclulistnode. prototype. getNext = function (){
Return this. prev;
};
Dare. parse listnode. prototype. setNext = function (node ){
This. prev = node;
};

Linked List. js linked listCopy codeThe Code is as follows :/*
* Two-way linked list
*/
Dare. Functions List = function (){
This. head = null;
This. current = null;
This. tail = null;
This. length = 0;
};
Dare. extend (Dare. extenlist, Dare );
/*
* Add a node using the tail plug-in method
*/
Dare. shortlist. 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 ++;
};
/*
* Delete A node
*/
Dare. inclulist. prototype. moveNode = function (node ){
If (this = null) return;
If (node = null) return;
// Intermediate node
Var prev = node. prev;
If (prev! = Null ){
Prev. next = node. next;
}
If (node. next! = Null ){
Node. next. prev = prev;
}
// Header Node
If (node = this. head ){
This. head = node. next;
}
// End Node
If (node = this. tail ){
If (prev! = Null ){
This. tail = prev;
}
Else {
This. head = this. tail;
}
}
Node. prev = null;
Node. next = null;
This. length --;
};
/*
* Construct a node
*/
Dare. shortlist. prototype. constructNode = function (node, obj ){
If (node = null | obj = null) return;
Node. data = obj;
Return node;
};
/*
* Get node data
*/
Dare. shortlist. prototype. getNodeData = function (node ){
If (node = null) return;
Return node. data;
};
/*
* Start from scratch
*/
Dare. shortlist. prototype. start = function (){
If (this = null) return;
Return this. current = this. head;
};
/*
* Starting from the end
*/
Dare. shortlist. prototype. end = function (){
If (this = null) return;
Return this. current = this. tail;
};
/*
* Next node
*/
Dare. shortlist. prototype. nextNode = function (){
If (this = null) return;
If (this. current = null) return
Var node = this. current;
This. current = this. current. next;
Return node;
};
/*
* Last Node
*/
Dare. shortlist. prototype. prevNode = function (){
If (this = null) return;
If (this. current = null) return
Var node = this. current;
This. current = this. current. prev;
Return node;
};
/*
* Empty linked list?
*/
Dare. shortlist. prototype. isempty = function (){
If (this = null) return true;
If (this. head = null ){
Return true;
}
Else {
Return false;
}
};
/*
* Chain table length
*/
Dare. shortlist. prototype. getLength = function (){
If (this = null) return;
Return this. length;
};
/*
* Clear the linked list
*/
Dare. shortlist. prototype. clearList = function (){
This. head. next = null;
This. head = null;
};
/*
* Whether a node exists
*/
Dare. Upload list. 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;
}
};

The actual call case code is updated one after another:Copy codeThe Code is as follows: <script type = "text/javascript">
Var rule list = new Dare. Rule List ();
Function createList (){
For (var I = 0; I <7; I ++ ){
Var movie = {};
Var inclulistnode = new Dare. inclulistnode ();
Movie. id = I;
Movie. name = 'movie _ '+ I;
Required listnode. data = movie;
Using list. appendNode (using listnode); // create a linked list
}
// DeleteNode (node list); // delete a node
// PrintList (linked list); // output linked list
PrintNode (upload list );
}
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 ){
Using list. moveNode (node); // deletes a specified 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 = listing list. getNodeData (node); // print the specified node
Document. writeln (movie. id + "<br> ");
Document. writeln (movie. name + "<br> ");
Break;
}
I ++;
Node = node. next;
}
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.