11# @datastructures-js/queue
22
3- [ ![ build:?] ( https://travis-ci.org/datastructures-js/queue.svg?branch=master )] ( https://travis-ci.org/datastructures-js/queue )
43[ ![ npm] ( https://img.shields.io/npm/v/@datastructures-js/queue.svg )] ( https://www.npmjs.com/package/@datastructures-js/queue )
54[ ![ npm] ( https://img.shields.io/npm/dm/@datastructures-js/queue.svg )] ( https://www.npmjs.com/packages/@datastructures-js/queue ) [ ![ npm] ( https://img.shields.io/badge/node-%3E=%206.0-blue.svg )] ( https://www.npmjs.com/package/@datastructures-js/queue )
65
@@ -14,16 +13,16 @@ A performant queue implementation in javascript.
1413* [ import] ( #import )
1514* [ API] ( #api )
1615 * [ constructor] ( #constructor )
17- * [ Queue.fromArray(elements) ] ( #queuefromarrayelements )
18- * [ . enqueue(element) ] ( #enqueueelement )
19- * [ . front() ] ( #front )
20- * [ . back() ] ( #back )
21- * [ . dequeue() ] ( #dequeue )
22- * [ . isEmpty() ] ( #isEmpty )
23- * [ . size() ] ( #size )
24- * [ . clone() ] ( #clone )
25- * [ . toArray() ] ( #toarray )
26- * [ . clear() ] ( #clear )
16+ * [ Queue.fromArray] ( #queuefromarray )
17+ * [ enqueue] ( #enqueue )
18+ * [ front] ( #front )
19+ * [ back] ( #back )
20+ * [ dequeue] ( #dequeue )
21+ * [ isEmpty] ( #isEmpty )
22+ * [ size] ( #size )
23+ * [ clone] ( #clone )
24+ * [ toArray] ( #toarray )
25+ * [ clear] ( #clear )
2726* [ Build] ( #build )
2827* [ License] ( #license )
2928
@@ -67,7 +66,7 @@ const queue = new Queue<number>();
6766const queue = new Queue < number> ([1 , 2 , 3 ]);
6867```
6968
70- ### Queue.fromArray(elements)
69+ ### Queue.fromArray
7170
7271##### JS
7372``` js
@@ -92,142 +91,63 @@ const list = [10, 3, 8, 40, 1];
9291const queue = Queue .fromArray < number> (list);
9392```
9493
95- ### . enqueue(element)
94+ ### enqueue
9695adds an element at the back of the queue.
9796
98- <table >
99- <tr >
100- <th align="center">params</th>
101- <th align="center">return</th>
102- <th align="center">runtime</th>
103- </tr >
104- <tr >
105- <td align="center">element: T</td>
106- <td align="center">Queue<T></td>
107- <td align="center">O(1)</td>
108- </tr >
109- </table >
110-
11197``` js
11298queue .enqueue (10 ).enqueue (20 );
11399```
114100
115- ### . front()
101+ ### front
116102peeks on the front element of the queue.
117103
118- <table >
119- <tr >
120- <th align="center">return</th>
121- <th align="center">runtime</th>
122- </tr >
123- <tr >
124- <td align="center">T</td>
125- <td align="center">O(1)</td>
126- </tr >
127- </table >
128-
129104``` js
130105console .log (queue .front ()); // 10
131106```
132107
133- ### . back()
108+ ### back
134109peeks on the back element in the queue.
135110
136- <table >
137- <tr >
138- <th align="center">return</th>
139- <th align="center">runtime</th>
140- </tr >
141- <tr >
142- <td align="center">T</td>
143- <td align="center">O(1)</td>
144- </tr >
145- </table >
146-
147111``` js
148112console .log (queue .back ()); // 20
149113```
150114
151- ### .dequeue()
152- dequeue the front element in the queue. It does not use * .shift()* to dequeue the element. Instead, it uses a pointer to get the front element and only remove elements when reaching half size of the queue.
153-
154- <table >
155- <tr >
156- <th align="center">return</th>
157- <th align="center">runtime</th>
158- </tr >
159- <tr >
160- <td align="center">T</td>
161- <td align="center">O(n*log(n))</td>
162- </tr >
163- </table >
115+ ### dequeue
116+ dequeue the front element in the queue.
164117
165118``` js
166119console .log (queue .dequeue ()); // 10
167120console .log (queue .front ()); // 20
168121```
169122
170- Dequeuing all elements takes <i >O(n\* log(n))</i > instead of <i >O(n<sup >2</sup >)</i > when using shift().
123+ Dequeuing all elements takes <i >O(n\* log(n))</i > instead of <i >O(n<sup >2</sup >)</i > when using shift/unshift with arrays.
171124
172125<b >benchmark:</b >
173126
174127dequeuing 1 million elements in Node v12
175128
176129<table >
177- <tr ><td >. dequeue() </td ><td >. shift() </td ></tr >
178- <tr ><td >~ 40 ms</td ><td >~ 3 minutes</td ></tr >
130+ <tr ><td >dequeue</td ><td >shift</td ></tr >
131+ <tr ><td >~40 ms</td ><td >~3 minutes</td ></tr >
179132</table >
180133
181- ### . isEmpty()
134+ ### isEmpty
182135checks if the queue is empty.
183136
184- <table >
185- <tr >
186- <th align="center">return</th>
187- <th align="center">runtime</th>
188- </tr >
189- <tr >
190- <td align="center">boolean</td>
191- <td align="center">O(1)</td>
192- </tr >
193- </table >
194-
195137``` js
196138console .log (queue .isEmpty ()); // false
197139```
198140
199- ### . size()
141+ ### size
200142returns the number of elements in the queue.
201143
202- <table >
203- <tr >
204- <th align="center">return</th>
205- <th align="center">runtime</th>
206- </tr >
207- <tr >
208- <td align="center">number</td>
209- <td align="center">O(1)</td>
210- </tr >
211- </table >
212-
213144``` js
214145console .log (queue .size ()); // 1
215146```
216147
217- ### . clone()
148+ ### clone
218149creates a shallow copy of the queue.
219150
220- <table >
221- <tr >
222- <th align="center">return</th>
223- <th align="center">runtime</th>
224- </tr >
225- <tr >
226- <td align="center">Queue<T></td>
227- <td align="center">O(n)</td>
228- </tr >
229- </table >
230-
231151``` js
232152const queue = Queue .fromArray ([{ id: 2 }, { id: 4 } , { id: 8 }]);
233153const clone = queue .clone ();
@@ -238,37 +158,17 @@ console.log(queue.front()); // { id: 2 }
238158console .log (clone .front ()); // { id: 4 }
239159```
240160
241- ### . toArray()
161+ ### toArray
242162returns a copy of the remaining elements as an array.
243163
244- <table >
245- <tr >
246- <th align="center">return</th>
247- <th align="center">runtime</th>
248- </tr >
249- <tr >
250- <td align="center">T[]</td>
251- <td align="center">O(n)</td>
252- </tr >
253- </table >
254-
255164``` js
256165queue .enqueue (4 ).enqueue (2 );
257166console .log (queue .toArray ()); // [20, 4, 2]
258167```
259168
260- ### . clear()
169+ ### clear
261170clears all elements from the queue.
262171
263- <table >
264- <tr >
265- <th >runtime</th >
266- </tr >
267- <tr >
268- <td >O(1)</td >
269- </tr >
270- </table >
271-
272172``` js
273173queue .clear ();
274174queue .size (); // 0
0 commit comments