File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Node {
2+ constructor ( data ) {
3+ this . data = data ;
4+ this . next = null ;
5+ }
6+ }
7+
8+ class Queue {
9+ constructor ( ) {
10+ this . head = null ;
11+ this . tail = null ;
12+ this . size = 0 ;
13+ }
14+
15+ push ( data ) {
16+ const node = new Node ( data ) ;
17+ if ( this . isEmpty ( ) ) {
18+ this . head = node ;
19+ this . head . next = this . tail ;
20+ } else this . tail . next = node ;
21+
22+ this . tail = node ;
23+ this . size += 1 ;
24+ }
25+
26+ pop ( ) {
27+ if ( this . isEmpty ( ) ) return null ;
28+ if ( this . head === this . tail ) this . tail = null ;
29+
30+ const headData = this . head . data ;
31+ this . head = this . head . next ;
32+ this . size -= 1 ;
33+
34+ return headData ;
35+ }
36+
37+ isEmpty ( ) {
38+ return this . size === 0 ;
39+ }
40+
41+ getArray ( ) {
42+ const array = [ ] ;
43+ if ( this . isEmpty ( ) ) return array ;
44+
45+ let currentNode = this . head ;
46+ while ( currentNode ) {
47+ array . push ( currentNode . data ) ;
48+ currentNode = currentNode . next ;
49+ }
50+
51+ return array ;
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments