Skip to content

Commit 07ab788

Browse files
committed
feat: implement pop, peek, and isEmpty methods in Stack class
1 parent 88c7821 commit 07ab788

File tree

1 file changed

+11
-3
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ public void push(int value) {
1515
}
1616

1717
public int pop() {
18-
return 0;
18+
if (isEmpty()) {
19+
throw new RuntimeException("Stack is empty");
20+
}
21+
int value = top.val;
22+
top = top.next;
23+
return value;
1924
}
2025

2126
public int peek() {
22-
return 0;
27+
if (isEmpty()) {
28+
throw new RuntimeException("Stack is empty");
29+
}
30+
return top.val;
2331
}
2432

2533
public boolean isEmpty() {
26-
return true;
34+
return top == null;
2735
}
2836
}

0 commit comments

Comments
 (0)