File tree Expand file tree Collapse file tree 5 files changed +128
-2
lines changed
lib/data-structures/linked-lists Expand file tree Collapse file tree 5 files changed +128
-2
lines changed Original file line number Diff line number Diff line change 77 {
88 "type" : " node" ,
99 "request" : " launch" ,
10- "name" : " HashMap" ,
11- "program" : " ${workspaceFolder}/src/runtimes/linear/hash-map.js"
10+ "name" : " Jest All" ,
11+ "program" : " ${workspaceFolder}/node_modules/jest/bin/jest" ,
12+ "args" : [" --runInBand" ],
13+ "console" : " integratedTerminal" ,
14+ "internalConsoleOptions" : " neverOpen"
15+ },
16+ {
17+ "type" : " node" ,
18+ "request" : " launch" ,
19+ "name" : " Jest Current File" ,
20+ "program" : " ${workspaceFolder}/node_modules/jest/bin/jest" ,
21+ "args" : [" ${relativeFile}" ],
22+ "console" : " integratedTerminal" ,
23+ "internalConsoleOptions" : " neverOpen"
1224 }
1325 ]
1426}
Original file line number Diff line number Diff line change @@ -75,6 +75,34 @@ class LinkedList {
7575 return target . value ;
7676 }
7777 }
78+
79+ /**
80+ * Find first occurence of the element matching the value
81+ * return index or undefined
82+ * Runtime: O(n)
83+ * @param {any } value
84+ */
85+ contains ( value ) {
86+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
87+ if ( current . value === value ) {
88+ return index ;
89+ }
90+ }
91+ }
92+
93+ /**
94+ * Remove the nth element from the list. Starting with 0
95+ * Returns value if found or undefined if it was not found
96+ * Runtime: O(n)
97+ * @param {any } nth
98+ */
99+ removeAt ( nth ) {
100+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
101+ if ( index === nth ) {
102+ return current . value ;
103+ }
104+ }
105+ }
78106}
79107
80108module . exports = LinkedList ;
Original file line number Diff line number Diff line change @@ -83,6 +83,34 @@ class LinkedList {
8383 return target . value ;
8484 }
8585 }
86+
87+ /**
88+ * Find first occurence of the element matching the value
89+ * return index or undefined
90+ * Runtime: O(n)
91+ * @param {any } value
92+ */
93+ contains ( value ) {
94+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
95+ if ( current . value === value ) {
96+ return index ;
97+ }
98+ }
99+ }
100+
101+ /**
102+ * Remove the nth element from the list. Starting with 0
103+ * Returns value if found or undefined if it was not found
104+ * Runtime: O(n)
105+ * @param {any } nth
106+ */
107+ removeAt ( nth ) {
108+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
109+ if ( index === nth ) {
110+ return current . value ;
111+ }
112+ }
113+ }
86114}
87115
88116// Aliases
Original file line number Diff line number Diff line change @@ -103,6 +103,34 @@ class LinkedList {
103103 return target . value ;
104104 }
105105 }
106+
107+ /**
108+ * Find first occurence of the element matching the value
109+ * return index or undefined
110+ * Runtime: O(n)
111+ * @param {any } value
112+ */
113+ contains ( value ) {
114+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
115+ if ( current . value === value ) {
116+ return index ;
117+ }
118+ }
119+ }
120+
121+ /**
122+ * Remove the nth element from the list. Starting with 0
123+ * Returns value if found or undefined if it was not found
124+ * Runtime: O(n)
125+ * @param {any } nth
126+ */
127+ removeAt ( nth ) {
128+ for ( let current = this . first , index = 0 ; current ; index ++ , current = current . next ) {
129+ if ( index === nth ) {
130+ return current . value ;
131+ }
132+ }
133+ }
106134}
107135
108136// Aliases
Original file line number Diff line number Diff line change @@ -90,6 +90,36 @@ describe('LinkedList', function () {
9090 } ) ;
9191 } ) ;
9292
93+ describe ( 'with elements' , ( ) => {
94+ beforeEach ( ( ) => {
95+ linkedList . addLast ( 0 ) ;
96+ linkedList . addLast ( 'found' ) ;
97+ } ) ;
98+
99+ describe ( '#contains' , ( ) => {
100+ it ( 'should find elements' , ( ) => {
101+ expect ( linkedList . contains ( 0 ) ) . toBe ( 0 ) ;
102+ expect ( linkedList . contains ( 'found' ) ) . toBe ( 1 ) ;
103+ } ) ;
104+
105+ it ( 'should return undefined' , ( ) => {
106+ expect ( linkedList . contains ( 'hola' ) ) . toBe ( undefined ) ;
107+ } ) ;
108+ } ) ;
109+
110+ describe ( '#removeAt' , ( ) => {
111+ it ( 'should remove element and return value' , ( ) => {
112+ expect ( linkedList . removeAt ( 1 ) ) . toBe ( 'found' ) ;
113+ expect ( linkedList . removeAt ( 0 ) ) . toBe ( 0 ) ;
114+ } ) ;
115+
116+ it ( 'should return undefined ' , ( ) => {
117+ expect ( linkedList . removeAt ( 2 ) ) . toBe ( undefined ) ;
118+ expect ( linkedList . removeAt ( - 2 ) ) . toBe ( undefined ) ;
119+ } ) ;
120+ } ) ;
121+ } ) ;
122+
93123 describe ( 'Doubly Linked List and aliases' , ( ) => {
94124
95125 describe ( '#addLast' , ( ) => {
You can’t perform that action at this time.
0 commit comments