File tree 1 file changed +6
-7
lines changed
src/_DataStructures_/DoublyLinkedList
1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ class Node {
4
4
this . data = data ;
5
5
this . previous = previous ;
6
6
this . next = next ;
7
+ this . length = 0 ;
7
8
}
8
9
}
9
10
@@ -21,20 +22,24 @@ class DoublyLinkedList {
21
22
const newNode = new Node ( value , this . head , this . head . next ) ;
22
23
this . head . next . previous = newNode ;
23
24
this . head . next = newNode ;
25
+ this . length += 1 ;
24
26
}
25
27
26
28
addAtEnd ( value ) {
27
29
const newNode = new Node ( value , this . tail . previous , this . tail ) ;
28
30
this . tail . previous . next = newNode ;
29
31
this . tail . previous = newNode ;
32
+ this . length += 1 ;
30
33
}
31
34
32
35
removeAtBeginning ( ) {
33
36
this . remove ( this . head . next ) ;
37
+ this . length -= 1 ;
34
38
}
35
39
36
40
removeAtEnd ( ) {
37
41
this . remove ( this . tail . previous ) ;
42
+ this . length -= 1 ;
38
43
}
39
44
40
45
remove ( node ) {
@@ -45,13 +50,7 @@ class DoublyLinkedList {
45
50
}
46
51
47
52
length ( ) {
48
- let address = this . head . next ;
49
- let count = 0 ;
50
- while ( address !== this . tail ) {
51
- count += 1 ;
52
- address = address . next ;
53
- }
54
- return count ;
53
+ return this . length ;
55
54
}
56
55
57
56
display ( ) {
You can’t perform that action at this time.
0 commit comments