1
+ import Stack from '../../Data-Structures/Sequential/Stack.ts' ;
2
+
3
+ // Ahhhhh! What a glorious day to do things just because we can! ^.^
4
+
5
+ class StackifiedQueue < T > {
6
+ private first : Stack < T > ; // Stack with elements ordered first-on-top
7
+ private last : Stack < T > ; // Stack with elements ordered last-on-top
8
+ private length : number ;
9
+
10
+ constructor ( ) {
11
+ this . first = new Stack ( ) ;
12
+ this . last = new Stack ( ) ;
13
+ this . length = 0 ;
14
+ }
15
+
16
+ public getlength ( ) : number {
17
+ return this . length ;
18
+ }
19
+
20
+ public isEmpty ( ) : boolean {
21
+ return this . length === 0 ;
22
+ }
23
+
24
+ public enqueue ( value : T ) : boolean {
25
+
26
+ // Transfer all items from first-on-top to last-on-top
27
+ this . _populateLastOnTop ( ) ;
28
+
29
+ // Add to end of line, i.e. push to last-on-top stack
30
+ this . last . push ( value ) ;
31
+ ++ this . length ;
32
+
33
+ return true ;
34
+ }
35
+
36
+ public dequeue ( ) : T | null {
37
+ if ( this . length === 0 ) return null ;
38
+
39
+ this . _populateFirstOnTop ( ) ;
40
+ -- this . length ;
41
+
42
+ return this . first . pop ( ) ;
43
+ }
44
+
45
+ public peek ( ) : T | null {
46
+ if ( this . length === 0 ) return null ;
47
+ this . _populateFirstOnTop ( ) ;
48
+ return this . first . peek ( ) ;
49
+ }
50
+
51
+ private _populateFirstOnTop ( ) : void {
52
+ if ( this . first . getLength ( ) > 0 ) return ;
53
+
54
+ for ( let i = 0 ; i < this . length ; ++ i ) {
55
+ this . first . push ( this . last . pop ( ) ) ;
56
+ }
57
+ }
58
+
59
+ private _populateLastOnTop ( ) : void {
60
+ if ( this . last . getLength ( ) > 0 ) return ;
61
+
62
+ for ( let i = 0 ; i < this . length ; ++ i ) {
63
+ this . last . push ( this . first . pop ( ) ) ;
64
+ }
65
+ }
66
+ }
67
+
68
+ function printQueue ( queue : StackifiedQueue < any > ) {
69
+ console . log ( JSON . stringify ( queue ) ) ;
70
+ }
71
+
72
+ function printPeekQueue ( queue : StackifiedQueue < any > ) {
73
+ console . log ( 'Peeking... Found' , JSON . stringify ( queue . peek ( ) ) ) ;
74
+ }
75
+
76
+ function printDequeue ( queue : StackifiedQueue < any > ) {
77
+ console . log ( 'Dequeued:' , JSON . stringify ( queue . dequeue ( ) ) ) ;
78
+ }
79
+
80
+
81
+ //---------------------------------------------------------------------
82
+ // ---------- MAIN PROGRAM ----------
83
+ //---------------------------------------------------------------------
84
+ if ( import . meta. main ) {
85
+
86
+ const ATLA = new StackifiedQueue < string > ( ) ;
87
+
88
+ printPeekQueue ( ATLA ) ;
89
+ ATLA . enqueue ( 'Sokka' ) ;
90
+ ATLA . enqueue ( 'Katara' ) ;
91
+ printPeekQueue ( ATLA ) ;
92
+ ATLA . enqueue ( 'Aang' ) ;
93
+ ATLA . enqueue ( 'Appa' ) ;
94
+
95
+ printQueue ( ATLA ) ;
96
+
97
+ printDequeue ( ATLA ) ;
98
+ printDequeue ( ATLA ) ;
99
+ printDequeue ( ATLA ) ;
100
+
101
+ printQueue ( ATLA ) ;
102
+
103
+ printDequeue ( ATLA ) ;
104
+
105
+ printQueue ( ATLA ) ;
106
+
107
+ ATLA . enqueue ( 'Zuko' ) ;
108
+ ATLA . enqueue ( 'Iroh' ) ;
109
+
110
+ printQueue ( ATLA ) ;
111
+
112
+ // RUN: deno run Playground/Challenges/StackifiedQueue.ts
113
+ }
114
+
115
+ // --------------------------- Terminal Output: ---------------------------
116
+ // Peeking... Found null
117
+ // Peeking... Found "Sokka"
118
+ // {"first":{"length":0,"values":[null,null]},"last":{"length":4,"values":["Sokka","Katara","Aang","Appa"]},"length":4 }
119
+ // Dequeued: "Sokka"
120
+ // Dequeued: "Katara"
121
+ // Dequeued: "Aang"
122
+ // {"first":{"length":1,"values":["Appa",null,null,null]},"last":{"length":0,"values":[null,null,null,null]},"length":1 }
123
+ // Dequeued: "Appa"
124
+ // {"first":{"length":0,"values":[null,null,null,null]},"last":{"length":0,"values":[null,null,null,null]},"length":0 }
125
+ // {"first":{"length":0,"values":[null,null,null,null]},"last":{"length":2,"values":["Zuko","Iroh",null,null]},"length":2 }
0 commit comments