File tree Expand file tree Collapse file tree 4 files changed +96
-11
lines changed
lib/data-structures/queues Expand file tree Collapse file tree 4 files changed +96
-11
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Data structure where add and remove elements in a first-in, first-out (FIFO)
3+ */
14class Queue {
25 constructor ( ) {
36 this . input = [ ] ;
47 }
58
9+ /**
10+ * Add element to the queue
11+ * Runtime: O(1)
12+ * @param {any } element
13+ */
614 add ( element ) {
715 this . input . push ( element ) ;
816 }
917
18+ /**
19+ * Add element to the queue
20+ * Runtime: O(1)
21+ * @param {any } element
22+ */
1023 remove ( ) {
1124 return this . input . shift ( ) ;
1225 }
26+
27+ /**
28+ * Size of the queue
29+ */
30+ get size ( ) {
31+ return this . input . length ;
32+ }
1333}
1434
1535module . exports = Queue ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Data structure where add and remove elements in a first-in, first-out (FIFO)
3+ */
4+ class Queue {
5+ constructor ( ) {
6+ this . input = [ ] ;
7+ this . output = [ ] ;
8+ }
9+
10+ /**
11+ * Add element to the queue
12+ * Runtime: O(1)
13+ * @param {any } element
14+ */
15+ add ( element ) {
16+ this . input . push ( element ) ;
17+ }
18+
19+ /**
20+ * Remove element from the queue
21+ * Runtime: O(n)
22+ * Amortized runtime: O(1)*
23+ */
24+ remove ( ) {
25+ if ( ! this . output . length ) {
26+ while ( this . input . length ) {
27+ this . output . push ( this . input . pop ( ) ) ;
28+ }
29+ }
30+ return this . output . pop ( ) ;
31+ }
32+
33+ /**
34+ * Size of the queue
35+ */
36+ get size ( ) {
37+ return this . input . length + this . output . length ;
38+ }
39+ }
40+
41+ // Aliases
42+ Queue . prototype . enqueue = Queue . prototype . add ;
43+ Queue . prototype . dequeue = Queue . prototype . remove ;
44+
45+ module . exports = Queue ;
Original file line number Diff line number Diff line change 1+ const LinkedList = require ( '../linked-lists/linked-list' ) ;
2+
3+ /**
4+ * Data structure where add and remove elements in a first-in, first-out (FIFO)
5+ */
16class Queue {
27 constructor ( ) {
3- this . input = [ ] ;
4- this . output = [ ] ;
8+ this . input = new LinkedList ( ) ;
59 }
610
11+ /**
12+ * Add element to the queue
13+ * Runtime: O(1)
14+ * @param {any } element
15+ */
716 add ( element ) {
8- this . input . push ( element ) ;
17+ this . input . addFirst ( element ) ;
918 }
1019
20+ /**
21+ * Remove element from the queue
22+ * Runtime: O(1)
23+ */
1124 remove ( ) {
12- if ( ! this . output . length ) {
13- while ( this . input . length ) {
14- this . output . push ( this . input . pop ( ) ) ;
15- }
16- }
17- return this . output . pop ( ) ;
25+ return this . input . removeLast ( ) ;
26+ }
27+
28+ /**
29+ * Size of the queue
30+ */
31+ get size ( ) {
32+ return this . input . size ;
1833 }
1934}
2035
36+ // Aliases
37+ Queue . prototype . enqueue = Queue . prototype . add ;
38+ Queue . prototype . dequeue = Queue . prototype . remove ;
39+
2140module . exports = Queue ;
Original file line number Diff line number Diff line change 1- const Queue = require ( './queue.js' ) ;
1+ const Queue = require ( './queue-2 .js' ) ;
22
33describe ( 'Queue' , function ( ) {
44 let queue ;
@@ -9,8 +9,9 @@ describe('Queue', function () {
99
1010 describe ( '#add' , ( ) => {
1111 it ( 'should push an element to the queue' , ( ) => {
12+ expect ( queue . size ) . toEqual ( 0 ) ;
1213 queue . add ( 1 ) ;
13- expect ( queue . input ) . toEqual ( [ 1 ] ) ;
14+ expect ( queue . size ) . toEqual ( 1 ) ;
1415 } ) ;
1516 } ) ;
1617
You can’t perform that action at this time.
0 commit comments