77 * @param item {*} The item to store in the node.
88 * @constructor
99 */
10- function Node ( item ) {
10+ function DLLNode ( item ) {
1111 /**
1212 * The item stored.
1313 * @type {* }
1414 */
1515 this . item = item ;
1616 /**
1717 * The next node. It's null if there's no a next node.
18- * @type {Node |null }
18+ * @type {DLLNode |null }
1919 */
2020 this . next = null ;
2121 /**
2222 * The previous node. It's null if there's no a previous node.
23- * @type {Node |null }
23+ * @type {DLLNode |null }
2424 */
2525 this . previous = null ;
2626}
@@ -32,12 +32,12 @@ function Node(item) {
3232function DoubleLinkedList ( ) {
3333 /**
3434 * The first node of the list.
35- * @type {Node |null }
35+ * @type {DLLNode |null }
3636 */
3737 this . first = null ;
3838 /**
3939 * The last node of the list.
40- * @type {Node |null }
40+ * @type {DLLNode |null }
4141 */
4242 this . last = null ;
4343 /**
@@ -60,7 +60,7 @@ DoubleLinkedList.prototype.getIterator = function () {
6060 * @return {void }
6161 */
6262DoubleLinkedList . prototype . pushFront = function ( item ) {
63- var node = new Node ( item ) ;
63+ var node = new DLLNode ( item ) ;
6464 node . next = this . first ;
6565 this . first = node ;
6666 //link the next node to the new node
@@ -77,7 +77,7 @@ DoubleLinkedList.prototype.pushFront = function (item) {
7777 * @return {void }
7878 */
7979DoubleLinkedList . prototype . pushBack = function ( item ) {
80- var node = new Node ( item ) ;
80+ var node = new DLLNode ( item ) ;
8181 node . previous = this . last ;
8282 this . last = node ;
8383 //link the previous node to the new node
@@ -89,8 +89,8 @@ DoubleLinkedList.prototype.pushBack = function (item) {
8989} ;
9090
9191/**
92- * Remove the first element of the list.
93- * @return {* } The element removed. It's undefined if the list is empty.
92+ * Remove the first item of the list.
93+ * @return {* } The item removed. It's undefined if the list is empty.
9494 */
9595DoubleLinkedList . prototype . popFront = function ( ) {
9696 if ( this . length ) {
@@ -106,8 +106,8 @@ DoubleLinkedList.prototype.popFront = function () {
106106} ;
107107
108108/**
109- * Remove the last element of the list.
110- * @return {* } The element removed. It's undefined if the list is empty.
109+ * Remove the last item of the list.
110+ * @return {* } The item removed. It's undefined if the list is empty.
111111 */
112112DoubleLinkedList . prototype . popBack = function ( ) {
113113 if ( this . length ) {
@@ -146,6 +146,25 @@ DoubleLinkedList.prototype.removeAt = function (index) {
146146 return node . item ;
147147} ;
148148
149+ /**
150+ * Delete the node from the list.
151+ * @param node {DLLNode} The node to delete.
152+ * @return {void }
153+ */
154+ DoubleLinkedList . prototype . deleteNode = function ( node ) {
155+ if ( node === this . first ) {
156+ this . popFront ( ) ;
157+ return ;
158+ }
159+ if ( node === this . last ) {
160+ this . popBack ( ) ;
161+ return ;
162+ }
163+ node . previous . next = node . next ;
164+ node . next . previous = node . previous ;
165+ this . length -- ;
166+ } ;
167+
149168/**
150169 * Get the item at the position index.
151170 * @param index {Number} The position of the item.
@@ -167,13 +186,12 @@ DoubleLinkedList.prototype.getItem = function (index) {
167186
168187/**
169188 * Get the node at the position index relative from the node.
170- * @param node {Node } The node from which start the search.
189+ * @param [ node = first] {DLLNode } The node from which start the search.
171190 * @param index {Number} The index, relative to the node, of the node to return.
172- * @return {Node } The node at the position index.
191+ * @return {DLLNode } The node at the position index.
173192 */
174193DoubleLinkedList . prototype . getNode = function ( node , index ) {
175- if ( ! node )
176- node = this . head ;
194+ node = node || this . first ;
177195 for ( ; index > 0 ; index -- )
178196 node = node . next ;
179197 return node ;
@@ -249,7 +267,10 @@ DoubleLinkedList.prototype.parallelSort = function () {
249267
250268/**
251269 * Sort the list.
252- * @param callback {function} The function invoked in order to get the value for the evaluation of the sort criteria. That parameter could be omitted. In that case, will be returned the same item.
270+ * @param [callback = function(item){return(item);}] {function} The function invoked in order to get the value for the evaluation of the sort criteria.
271+ * @example
272+ * callback = function(item) {return -item.key;}
273+ * This function callback will return the opposite of the attribute key of the item. In this case the list will be sorted in descending order.
253274 * @return {void }
254275 */
255276DoubleLinkedList . prototype . sort = function ( callback ) {
0 commit comments