File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed
lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12 Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -9,18 +9,39 @@ public Stack() {
99 }
1010
1111 public void push (int value ) {
12- // Your code here
12+ ListNode newNode = new ListNode (value );
13+ // putting ontop of stack
14+ newNode .next = top ;
15+ // setting top as new node
16+ top = newNode ;
1317 }
1418
1519 public int pop () {
16- return 0 ;
20+ // checking to see if top is null
21+ if (top == null ) {
22+ throw new IllegalStateException ("stack is empty" );
23+ }
24+ // created a varible to store current value
25+ int value = top .val ;
26+ // remove the top
27+ top = top .next ;
28+ return value ;
1729 }
1830
1931 public int peek () {
20- return 0 ;
32+ if (top == null ) {
33+ throw new IllegalStateException ("stack is empty" );
34+ }
35+
36+ return top .val ;
2137 }
2238
2339 public boolean isEmpty () {
24- return true ;
40+ if (top == null ) {
41+ return true ;
42+ } else {
43+ return false ;
44+ }
2545 }
2646}
47+
You can’t perform that action at this time.
0 commit comments