Actual code:
linkedlistnode.js node class
/*
* Linked list node
*/
Dare.LinkedListNode = function () {
this.data = null;//Data field
this.prev = null;//Precursor
this.next = null;//Backdrive
};
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
/*
* Doubly linked list
*/
Dare.LinkedList = function () {
this.head = null;
this.current = null;
this.tail = null;
this.length = 0;
};
Dare.extend(Dare.LinkedList, Dare);
/*
* Tail interpolation method adds nodes
*/
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 scratch
*/
Dare.LinkedList.prototype.start = function () {
if (this == null) return;
return this.current = this.head;
};
/*
* start from 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;
};
/*
* Previous 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 linked list
*/
Dare.LinkedList.prototype.clearList = function () {
this.head.next = null;
this.head = null;
};
/*
* Whether node exists
*/
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 use case code is updated one after another: