@@ -5,44 +5,21 @@ class Node {
5
5
* Creates a new Node with the given element.
6
6
* @param {* } element - The element to be stored in the node.
7
7
*/
8
- constructor ( element ) {
9
- this . element = element
10
- this . next = null
11
- this . prev = null
8
+ constructor ( element ) {
9
+ this . element = element ;
10
+ this . next = null ;
11
+ this . prev = null ;
12
12
}
13
13
}
14
14
15
15
class CircularDoublyLinkedList {
16
16
/**
17
17
* Creates an empty Circular Doubly Linked List.
18
18
*/
19
- constructor ( ) {
20
- this . length = 0
21
- this . head = null
22
- this . tail = null
23
- }
24
-
25
- /**
26
- * Appends an element to the end of the list.
27
- * @param {* } element - The element to be appended.
28
- */
29
- append ( element ) {
30
- const node = new Node ( element )
31
-
32
- if ( ! this . head ) {
33
- this . head = node
34
- this . tail = node
35
- node . next = node // Circular reference
36
- node . prev = node // Circular reference
37
- } else {
38
- node . prev = this . tail
39
- node . next = this . head
40
- this . tail . next = node
41
- this . head . prev = node
42
- this . tail = node
43
- }
44
-
45
- this . length ++
19
+ constructor ( ) {
20
+ this . length = 0 ;
21
+ this . head = null ;
22
+ this . tail = null ;
46
23
}
47
24
48
25
/**
@@ -51,98 +28,113 @@ class CircularDoublyLinkedList {
51
28
* @param {* } element - The element to be inserted.
52
29
* @returns {boolean } - True if the insertion was successful, false otherwise.
53
30
*/
54
- insert ( position , element ) {
31
+ insertAt ( position , element ) {
55
32
if ( position >= 0 && position <= this . length ) {
56
- const node = new Node ( element )
57
- let current = this . head
58
- let previous = null
59
- let index = 0
33
+ const node = new Node ( element ) ;
34
+ let current = this . head ;
35
+ let previous = null ;
36
+ let index = 0 ;
60
37
61
38
if ( position === 0 ) {
62
- node . next = current
63
- node . prev = this . tail
64
- this . head = node
65
- current . prev = node
66
- this . tail . next = node
39
+ if ( ! this . head ) {
40
+ this . head = node ;
41
+ this . tail = node ;
42
+ node . next = node ; // Circular reference
43
+ node . prev = node ; // Circular reference
44
+ } else {
45
+ node . next = current ;
46
+ node . prev = this . tail ;
47
+ this . head = node ;
48
+ current . prev = node ;
49
+ this . tail . next = node ;
50
+ }
67
51
} else {
68
52
while ( index ++ < position ) {
69
- previous = current
70
- current = current . next
53
+ previous = current ;
54
+ current = current . next ;
71
55
}
72
- node . next = current
73
- node . prev = previous
74
- previous . next = node
75
- current . prev = node
56
+ node . next = current ;
57
+ node . prev = previous ;
58
+ previous . next = node ;
59
+ current . prev = node ;
76
60
77
61
if ( position === this . length ) {
78
- this . tail = node
62
+ this . tail = node ;
79
63
}
80
64
}
81
65
82
- this . length ++
83
- return true
66
+ this . length ++ ;
67
+ return true ;
84
68
} else {
85
- return false
69
+ return false ;
86
70
}
87
71
}
88
72
73
+ /**
74
+ * Appends an element to the end of the list.
75
+ * @param {* } element - The element to be appended.
76
+ */
77
+ append ( element ) {
78
+ return this . insertAt ( this . length , element ) ;
79
+ }
80
+
89
81
/**
90
82
* Removes and returns the element at the specified position.
91
83
* @param {number } position - The position of the element to be removed.
92
84
* @returns {* } - The removed element, or null if the position is invalid.
93
85
*/
94
- removeAt ( position ) {
86
+ removeAt ( position ) {
95
87
if ( position >= 0 && position < this . length ) {
96
- let current = this . head
97
- let previous = null
98
- let index = 0
88
+ let current = this . head ;
89
+ let previous = null ;
90
+ let index = 0 ;
99
91
100
92
if ( position === 0 ) {
101
- this . head = current . next
102
- this . head . prev = this . tail
103
- this . tail . next = this . head
93
+ this . head = current . next ;
94
+ this . head . prev = this . tail ;
95
+ this . tail . next = this . head ;
104
96
if ( this . length === 1 ) {
105
- this . tail = null
97
+ this . tail = null ;
106
98
}
107
99
} else {
108
100
while ( index ++ < position ) {
109
- previous = current
110
- current = current . next
101
+ previous = current ;
102
+ current = current . next ;
111
103
}
112
- previous . next = current . next
113
- current . next . prev = previous
104
+ previous . next = current . next ;
105
+ current . next . prev = previous ;
114
106
115
107
if ( position === this . length - 1 ) {
116
- this . tail = previous
108
+ this . tail = previous ;
117
109
}
118
110
}
119
111
120
- this . length --
121
- return current . element
112
+ this . length -- ;
113
+ return current . element ;
122
114
} else {
123
- return null
115
+ return null ;
124
116
}
125
117
}
126
118
127
119
/**
128
120
* Iterator over the elements in the list.
129
121
*/
130
122
* elements ( ) {
131
- let currentNode = this . head
132
- if ( ! currentNode ) return
123
+ let currentNode = this . head ;
124
+ if ( ! currentNode ) return ;
133
125
do {
134
- yield currentNode . element
135
- currentNode = currentNode . next
136
- } while ( currentNode !== this . head )
126
+ yield currentNode . element ;
127
+ currentNode = currentNode . next ;
128
+ } while ( currentNode !== this . head ) ;
137
129
}
138
130
139
131
/**
140
132
* Checks if the list is empty.
141
133
* @returns {boolean } - True if the list is empty, false otherwise.
142
134
*/
143
- isEmpty ( ) {
144
- return this . length === 0
135
+ isEmpty ( ) {
136
+ return this . length === 0 ;
145
137
}
146
138
}
147
139
148
- export { CircularDoublyLinkedList }
140
+ export { CircularDoublyLinkedList } ;
0 commit comments