Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84feda9

Browse files
committed
--refactor : refactor length function
1 parent 6855dce commit 84feda9

File tree

1 file changed

+6
-7
lines changed
  • src/_DataStructures_/DoublyLinkedList

1 file changed

+6
-7
lines changed

‎src/_DataStructures_/DoublyLinkedList/index.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Node {
44
this.data = data;
55
this.previous = previous;
66
this.next = next;
7+
this.length = 0;
78
}
89
}
910

@@ -21,20 +22,24 @@ class DoublyLinkedList {
2122
const newNode = new Node(value, this.head, this.head.next);
2223
this.head.next.previous = newNode;
2324
this.head.next = newNode;
25+
this.length += 1;
2426
}
2527

2628
addAtEnd(value) {
2729
const newNode = new Node(value, this.tail.previous, this.tail);
2830
this.tail.previous.next = newNode;
2931
this.tail.previous = newNode;
32+
this.length += 1;
3033
}
3134

3235
removeAtBeginning() {
3336
this.remove(this.head.next);
37+
this.length -= 1;
3438
}
3539

3640
removeAtEnd() {
3741
this.remove(this.tail.previous);
42+
this.length -= 1;
3843
}
3944

4045
remove(node) {
@@ -45,13 +50,7 @@ class DoublyLinkedList {
4550
}
4651

4752
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;
5554
}
5655

5756
display() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /