Realization of _javascript skills by using JS to realize bidirectional linked list Internet STB

Source: Internet
Author: User
Tags prev
On the actual code:
Linkedlistnode.js Node class
Copy Code code as follows:

/*
* Linked list node
*/
Dare.linkedlistnode = function () {
This.data = null;//data field
This.prev = null;//Precursor
This.next = null;//Rear Drive
};
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 Linked List class
Copy Code code as follows:

/*
* Two-way linked list
*/
Dare.linkedlist = function () {
This.head = null;
This.current = null;
This.tail = null;
this.length = 0;
};
Dare.extend (Dare.linkedlist, Dare);
/*
* Add nodes to the tail interpolation method
*/
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++;
};
/*
* Delete Node
*/
Dare.LinkedList.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;
}
Head node
if (node = = This.head) {
This.head = Node.next;
}
Tail 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 node
*/
Dare.LinkedList.prototype.constructNode = function (node, obj) {
if (node = null | | obj = null) return;
Node.data = obj;
return node;
};
/*
* Get Node data
*/
Dare.LinkedList.prototype.getNodeData = function (node) {
if (node = null) return;
return node.data;
};
/*
* Start from the beginning
*/
Dare.LinkedList.prototype.start = function () {
if (this = null) return;
return this.current = This.head;
};
/*
* Start at the end
*/
Dare.LinkedList.prototype.end = function () {
if (this = null) return;
return this.current = This.tail;
};
/*
* Next Node
*/
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;
};
/*
* Last 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;
};
/*
* Whether the linked list is empty
*/
Dare.LinkedList.prototype.isempty = function () {
if (this = null) return true;
if (This.head = = null) {
return true;
}
else {
return false;
}
};
/*
* Linked list length
*/
Dare.LinkedList.prototype.getLength = function () {
if (this = null) return;
return this.length;
};
/*
* Clear the list
*/
Dare.LinkedList.prototype.clearList = function () {
This.head.next = null;
This.head = null;
};
/*
* Is there a node
*/
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;
}
};

The actual call case code is updated continuously:
Copy Code code as follows:

<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); Create a linked list
}
Deletenode (LinkedList);//Delete node
Printlist (LinkedList); Output linked list
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); Delete 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 = Linkedlist.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.