File tree Expand file tree Collapse file tree 1 file changed +12
-7
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson12 ;
2
2
3
+ import java .util .EmptyStackException ;
4
+
3
5
/** Implement the below Stack by providing code for the class methods. */
4
6
public class Stack {
5
7
private ListNode top ;
@@ -16,17 +18,20 @@ public void push(int value) {
16
18
}
17
19
18
20
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 ;
21
27
}
22
- return 1 ;
23
28
}
24
29
25
30
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 ;
30
35
}
31
36
32
37
public boolean isEmpty () {
You can’t perform that action at this time.
0 commit comments