Skip to content

Commit 565c911

Browse files
Implemented Queue from stacks (CoffeelessProgrammer#4)
1 parent 9a6c460 commit 565c911

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

Data-Structures/Sequential/Queue.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default class Queue<T> {
1616
return this.length;
1717
}
1818

19+
public isEmpty(): boolean {
20+
return this.length === 0;
21+
}
22+
1923
public peek(): T | null {
2024
return this.first?.getValue() || null;
2125
}

Data-Structures/Sequential/Stack.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export default class Stack<T> {
1111
return this.length;
1212
}
1313

14+
public isEmpty(): boolean {
15+
return this.length === 0;
16+
}
17+
1418
public peek(): T | null {
1519
return this.values[this.length-1] || null;
1620
}

Data-Structures/Sequential/StackLL.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default class StackLL<T> {
1616
return this.length;
1717
}
1818

19+
public isEmpty(): boolean {
20+
return this.length === 0;
21+
}
22+
1923
public peek(): T | null {
2024
return this.top?.getValue() || null;
2125
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)