Skip to content

Commit 82d9e27

Browse files
committed
Feat: Implementing Stack
1 parent ac458a6 commit 82d9e27

File tree

1 file changed

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

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff 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+

0 commit comments

Comments
 (0)