File tree Expand file tree Collapse file tree 4 files changed +42
-1
lines changed
lib/data-structures/linked-lists Expand file tree Collapse file tree 4 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -97,8 +97,16 @@ class LinkedList {
9797 * @param {any } nth
9898 */
9999 removeAt ( nth ) {
100+ if ( nth === 0 ) {
101+ return this . removeFirst ( ) ;
102+ }
103+
100104 for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
101105 if ( index === nth ) {
106+ if ( ! current . next ) { // if it doesn't have next it means that it is the last
107+ return this . removeLast ( ) ;
108+ }
109+ current . previous = current . next ;
102110 return current . value ;
103111 }
104112 }
Original file line number Diff line number Diff line change @@ -105,8 +105,17 @@ class LinkedList {
105105 * @param {any } nth
106106 */
107107 removeAt ( nth ) {
108+ if ( nth === 0 ) {
109+ return this . removeFirst ( ) ;
110+ }
111+
108112 for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
109113 if ( index === nth ) {
114+ if ( ! current . next ) { // if it doesn't have next it means that it is the last
115+ return this . removeLast ( ) ;
116+ }
117+ current . previous = current . next ;
118+ this . size -- ;
110119 return current . value ;
111120 }
112121 }
Original file line number Diff line number Diff line change @@ -125,18 +125,29 @@ class LinkedList {
125125 * @param {any } nth
126126 */
127127 removeAt ( nth ) {
128+ if ( nth === 0 ) {
129+ return this . removeFirst ( ) ;
130+ }
131+
128132 for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
129133 if ( index === nth ) {
134+ if ( ! current . next ) { // if it doesn't have next it means that it is the last
135+ return this . removeLast ( ) ;
136+ }
137+ current . previous = current . next ;
138+ this . size -- ;
130139 return current . value ;
131140 }
132141 }
133142 }
143+
134144}
135145
136146// Aliases
137147LinkedList . prototype . add = LinkedList . prototype . push = LinkedList . prototype . addLast ;
138148LinkedList . prototype . remove = LinkedList . prototype . pop = LinkedList . prototype . removeLast ;
139149LinkedList . prototype . unshift = LinkedList . prototype . addFirst ;
140150LinkedList . prototype . shift = LinkedList . prototype . removeFirst ;
151+ LinkedList . prototype . search = LinkedList . prototype . contains ;
141152
142153module . exports = LinkedList ;
Original file line number Diff line number Diff line change @@ -109,14 +109,27 @@ describe('LinkedList', function () {
109109
110110 describe ( '#removeAt' , ( ) => {
111111 it ( 'should remove element and return value' , ( ) => {
112+ expect ( linkedList . size ) . toBe ( 2 ) ;
112113 expect ( linkedList . removeAt ( 1 ) ) . toBe ( 'found' ) ;
114+ expect ( linkedList . size ) . toBe ( 1 ) ;
115+
113116 expect ( linkedList . removeAt ( 0 ) ) . toBe ( 0 ) ;
117+ expect ( linkedList . size ) . toBe ( 0 ) ;
114118 } ) ;
115119
116- it ( 'should return undefined ' , ( ) => {
120+ it ( 'should return undefined if not found ' , ( ) => {
117121 expect ( linkedList . removeAt ( 2 ) ) . toBe ( undefined ) ;
118122 expect ( linkedList . removeAt ( - 2 ) ) . toBe ( undefined ) ;
119123 } ) ;
124+
125+ it ( 'should update size, last and first' , ( ) => {
126+ expect ( linkedList . removeAt ( 0 ) ) . toBe ( 0 ) ;
127+ expect ( linkedList . removeAt ( 0 ) ) . toBe ( 'found' ) ;
128+ expect ( linkedList . removeAt ( 0 ) ) . toBe ( undefined ) ;
129+ expect ( linkedList . size ) . toBe ( 0 ) ;
130+ expect ( linkedList . first ) . toBe ( null ) ;
131+ expect ( linkedList . last ) . toBe ( null ) ;
132+ } ) ;
120133 } ) ;
121134 } ) ;
122135
You can’t perform that action at this time.
0 commit comments