Skip to content

Commit 4700bdb

Browse files
author
AmiyahJo
committed
fix: Code rewrite in stack.java and an exception reccomended by vscode
1 parent 77ecfc5 commit 4700bdb

File tree

1 file changed

+12
-7
lines changed
  • lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.codedifferently.lesson12;
22

3+
import java.util.EmptyStackException;
4+
35
/** Implement the below Stack by providing code for the class methods. */
46
public class Stack {
57
private ListNode top;
@@ -16,17 +18,20 @@ public void push(int value) {
1618
}
1719

1820
public int pop() {
19-
if (isEmpty()){
20-
System.out.println("EMPTY");
21+
if (isEmpty()) {
22+
return 0;
23+
} else {
24+
int value = top.val;
25+
top = top.next;
26+
return value;
2127
}
22-
return 1;
2328
}
2429

2530
public int peek() {
26-
if(isEmpty()){
27-
//do stuff
28-
}
29-
return 1;
31+
if (isEmpty()) {
32+
throw new EmptyStackException();
33+
}
34+
return top.val;
3035
}
3136

3237
public boolean isEmpty() {

0 commit comments

Comments
 (0)