44
55/**
66 * The single node of the list.
7- * @param item The item to store in the node.
7+ * @param item {*} The item to store in the node.
88 * @constructor
99 */
1010function Node ( item ) {
1111 /**
1212 * The item stored.
13- * @type {Object }
13+ * @type {* }
1414 */
1515 this . item = item ;
1616 /**
@@ -20,6 +20,9 @@ function Node(item) {
2020 this . next = null ;
2121}
2222
23+ LinkedList . prototype = new Aggregate ( ) ;
24+ LinkedList . prototype . constructor = LinkedList ;
25+
2326/**
2427 * Class for managing a linked list.
2528 * @constructor
@@ -42,9 +45,16 @@ function LinkedList() {
4245 this . length = 0 ;
4346}
4447
48+ /**
49+ * @inheritDoc
50+ */
51+ LinkedList . prototype . getIterator = function ( ) {
52+ return new LinkedListIterator ( this ) ;
53+ } ;
54+
4555/**
4656 * Add an item at the head of the list.
47- * @param item The item to add.
57+ * @param item {*} The item to add.
4858 * @return {void }
4959 */
5060LinkedList . prototype . pushFront = function ( item ) {
@@ -58,12 +68,12 @@ LinkedList.prototype.pushFront = function (item) {
5868
5969/**
6070 * Add an item at the tail of the list.
61- * @param item The item to add.
71+ * @param item {*} The item to add.
6272 * @return {void }
6373 */
6474LinkedList . prototype . pushBack = function ( item ) {
6575 var node = new Node ( item ) ;
66- if ( this . last )
76+ if ( this . last )
6777 this . last . next = node ;
6878 else
6979 this . first = node ;
@@ -73,10 +83,10 @@ LinkedList.prototype.pushBack = function (item) {
7383
7484/**
7585 * Remove the first element of the list.
76- * @return {Object|undefined } The element removed. It's undefined if the list is empty.
86+ * @return {* } The element removed. It's undefined if the list is empty.
7787 */
78- LinkedList . prototype . popFront = function ( ) {
79- if ( this . length ) {
88+ LinkedList . prototype . popFront = function ( ) {
89+ if ( this . length ) {
8090 var node = this . first ;
8191 this . first = this . first . next ;
8292 this . length -- ;
@@ -88,16 +98,16 @@ LinkedList.prototype.popFront = function() {
8898
8999/**
90100 * Remove the last element of the list.
91- * @return {Object|undefined } The element removed. It's undefined if the list is empty.
101+ * @return {* } The element removed. It's undefined if the list is empty.
92102 */
93- LinkedList . prototype . popBack = function ( ) {
94- if ( this . length ) {
103+ LinkedList . prototype . popBack = function ( ) {
104+ if ( this . length ) {
95105 var node = this . last ;
96106 var next = this . first ;
97- while ( next . next && next . next . next ) {
107+ while ( next . next && next . next . next ) {
98108 next = next . next ;
99109 }
100- if ( node === next )
110+ if ( node === next )
101111 this . last = null ;
102112 else
103113 this . last = next ;
@@ -110,40 +120,82 @@ LinkedList.prototype.popBack = function() {
110120
111121/**
112122 * Remove the item at the position index.
113- * @param index The position of the item to remove.
114- * @return {Object|undefined }
123+ * @param index {Number} The position of the item to remove.
124+ * @return {* } The item stored at the position index. It's undefined if the index is out of bounds.
115125 */
116- LinkedList . prototype . removeAt = function ( index ) {
117- if ( index < 0 || index > this . length - 1 )
126+ LinkedList . prototype . removeAt = function ( index ) {
127+ if ( index < 0 || index > this . length - 1 )
118128 return undefined ;
119- if ( index === 0 )
129+ if ( index === 0 )
120130 return this . popFront ( ) ;
121- if ( index === this . length - 1 )
131+ if ( index === this . length - 1 )
122132 return this . popBack ( ) ;
123133 var node = this . first ;
124- for ( ; index > 1 ; index -- )
134+ for ( ; index > 1 ; index -- )
125135 node = node . next ;
126136 //now node is the node before the node to remove
127137 //node to remove
128138 var next = node . next ;
129- if ( next === this . last )
139+ if ( next === this . last )
130140 this . last = node ;
131- //noinspection JSPrimitiveTypeWrapperUsage
141+
132142 node . next = next . next ;
133143 this . length -- ;
134144 return next . item ;
135145} ;
136146
137147/**
138148 * Get the item at the position index.
139- * @param index The position of the item.
140- * @return {Object|undefined } . It's undefined if index isn't in the queue bounds.
149+ * @param index {Number} The position of the item.
150+ * @return {* } The item stored at the position index . It's undefined if index isn't in the queue bounds.
141151 */
142- LinkedList . prototype . getItem = function ( index ) {
143- if ( index < 0 || index > this . length )
152+ LinkedList . prototype . getItem = function ( index ) {
153+ if ( index < 0 || index > this . length - 1 )
144154 return undefined ;
145155 var node = this . first ;
146- for ( ; index > 0 ; index -- )
156+ for ( ; index > 0 ; index -- )
147157 node = node . next ;
148158 return node . item ;
159+ } ;
160+
161+ /**
162+ * Transform the list into an array.
163+ * @return {Array<*> } The array built.
164+ */
165+ LinkedList . prototype . toArray = function ( ) {
166+ var array = [ ] ;
167+ for ( var node = this . first , i = 0 ; node ; node = node . next , i ++ )
168+ array [ i ] = node . item ;
169+ return array ;
170+ } ;
171+
172+ /**
173+ * Build the list from the array.
174+ * @param array {Array<*>} The array from which build the list.
175+ * @return {void }
176+ */
177+ LinkedList . prototype . fromArray = function ( array ) {
178+ var node = this . first ;
179+ for ( var i = 0 ; i < Math . min ( this . length , array . length ) ; i ++ , node = node . next )
180+ node . item = array [ i ] ;
181+ if ( this . length < array . length )
182+ for ( var j = this . length ; j < array . length ; j ++ )
183+ this . pushBack ( array [ j ] ) ;
184+ else
185+ for ( var k = array . length ; k < this . length ; )
186+ this . popBack ( ) ;
187+ } ;
188+
189+ /**
190+ * Return the items that satisfy the condition determined by the callback.
191+ * @param callback {function} The function that implements the condition.
192+ * @return {Array<*> } The array that contains the items that satisfy the condition.
193+ */
194+ LinkedList . prototype . filter = function ( callback ) {
195+ var result = [ ] ;
196+ for ( var node = this . first ; node ; node = node . next ) {
197+ if ( callback ( node . item ) )
198+ result . push ( node . item ) ;
199+ }
200+ return result ;
149201} ;
0 commit comments