Skip to content

Commit 48f21eb

Browse files
committed
feat: adds Kimberlee's lesson 12 Stack.java file
1 parent ed5ac51 commit 48f21eb

File tree

1 file changed

+16
-4
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+16
-4
lines changed

lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@ public Stack() {
1010

1111
public void push(int value) {
1212
// Your code here
13+
ListNode newNode = ListNode(value);
14+
newNode.next = top;
15+
top = newNode;
1316
}
1417

1518
public int pop() {
16-
return 0;
19+
if (isEmpty()) {
20+
throw new RuntimeException("Stack is empty! Cannot pop.");
21+
}
22+
int value = top.val;
23+
top = top.next;
24+
return value;
1725
}
1826

1927
public int peek() {
20-
return 0;
28+
if (isEmpty()) {
29+
throw new RuntimeException("Stack is empty! Cannot peek.");
30+
}
31+
return top.val;
2132
}
22-
33+
2334
public boolean isEmpty() {
24-
return true;
35+
return top == null;
2536
}
2637
}
38+

0 commit comments

Comments
 (0)