@@ -5,44 +5,21 @@ class Node {
55 * Creates a new Node with the given element.
66 * @param {* } element - The element to be stored in the node.
77 */
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 ;
1212 }
1313}
1414
1515class CircularDoublyLinkedList {
1616 /**
1717 * Creates an empty Circular Doubly Linked List.
1818 */
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 ;
4623 }
4724
4825 /**
@@ -51,98 +28,113 @@ class CircularDoublyLinkedList {
5128 * @param {* } element - The element to be inserted.
5229 * @returns {boolean } - True if the insertion was successful, false otherwise.
5330 */
54- insert ( position , element ) {
31+ insertAt ( position , element ) {
5532 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 ;
6037
6138 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+ }
6751 } else {
6852 while ( index ++ < position ) {
69- previous = current
70- current = current . next
53+ previous = current ;
54+ current = current . next ;
7155 }
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 ;
7660
7761 if ( position === this . length ) {
78- this . tail = node
62+ this . tail = node ;
7963 }
8064 }
8165
82- this . length ++
83- return true
66+ this . length ++ ;
67+ return true ;
8468 } else {
85- return false
69+ return false ;
8670 }
8771 }
8872
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+ 8981 /**
9082 * Removes and returns the element at the specified position.
9183 * @param {number } position - The position of the element to be removed.
9284 * @returns {* } - The removed element, or null if the position is invalid.
9385 */
94- removeAt ( position ) {
86+ removeAt ( position ) {
9587 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 ;
9991
10092 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 ;
10496 if ( this . length === 1 ) {
105- this . tail = null
97+ this . tail = null ;
10698 }
10799 } else {
108100 while ( index ++ < position ) {
109- previous = current
110- current = current . next
101+ previous = current ;
102+ current = current . next ;
111103 }
112- previous . next = current . next
113- current . next . prev = previous
104+ previous . next = current . next ;
105+ current . next . prev = previous ;
114106
115107 if ( position === this . length - 1 ) {
116- this . tail = previous
108+ this . tail = previous ;
117109 }
118110 }
119111
120- this . length --
121- return current . element
112+ this . length -- ;
113+ return current . element ;
122114 } else {
123- return null
115+ return null ;
124116 }
125117 }
126118
127119 /**
128120 * Iterator over the elements in the list.
129121 */
130122 * elements ( ) {
131- let currentNode = this . head
132- if ( ! currentNode ) return
123+ let currentNode = this . head ;
124+ if ( ! currentNode ) return ;
133125 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 ) ;
137129 }
138130
139131 /**
140132 * Checks if the list is empty.
141133 * @returns {boolean } - True if the list is empty, false otherwise.
142134 */
143- isEmpty ( ) {
144- return this . length === 0
135+ isEmpty ( ) {
136+ return this . length === 0 ;
145137 }
146138}
147139
148- export { CircularDoublyLinkedList }
140+ export { CircularDoublyLinkedList } ;
0 commit comments