Skip to content

Commit 4f9e138

Browse files
author
AmiyahJo
committed
feat: adds typescript version of code for stack.ts
1 parent 992be27 commit 4f9e138

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lesson_12/structs_ts/src/stack.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,43 @@ export class Stack {
99

1010
push(value: number): void {
1111
// ListNode newNode = new ListNode(value);
12+
const newNode = new ListNode(value)
1213
// newNode.next = top;
14+
newNode.next = this.top;
1315
// top = newNode;
16+
this.top = newNode;
1417
}
1518

1619
pop(): number | undefined {
17-
return 0;
1820
// if (this.isEmpty()) {
1921
// throw new EmptyStackException();
2022
// } else {
2123
// int value = top.val;
2224
// top = top.next;
2325
// return value;
2426
// }
27+
if (this.isEmpty()) {
28+
return undefined;
29+
}
30+
31+
const value = this.top?.val;
32+
this.top = this.top.next;
33+
return value;
2534
}
2635

2736
peek(): number | null {
28-
return 0;
2937
// if (this.isEmpty()) {
3038
// throw new EmptyStackException();
3139
// }
3240
// return top.val;
41+
if (this.isEmpty()) {
42+
return undefined;
43+
}
44+
45+
return this.top?.val;
3346
}
3447

3548
isEmpty(): boolean {
36-
return top == null;
49+
return this.top === undefined;
3750
}
3851
}

0 commit comments

Comments
 (0)