forked from CI2692-AJ2022/interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.java
More file actions
45 lines (40 loc) · 1.38 KB
/
MyStack.java
File metadata and controls
45 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Arrays;
public class MyStack implements MyStackInterface {
public int[] stack = new int[0];
@Override
// Creates Stack push() method with arrays. Accepts the parameter 'item'
// that refers to the element to be pushed into the array.
public void push(int item){
int[] newArray = new int[stack.length+1];
newArray[stack.length] = item;
for(int i=0; i<stack.length; i++){
newArray[i] = stack[i];
}
stack= newArray;
}
@Override
// Stack pop() method with arrays. Pop the top element of the stack (the
// last integer of the list) and removes it from the same.
public Integer pop() {
if (stack.length > 0) {
int numberRemoved = stack[stack.length - 1];
int[] newArray = new int[stack.length - 1];
for (int i = 0; i < newArray.length; i++) {
newArray[i] = stack[i];
}
stack = newArray;
return numberRemoved;
} else {
return null; // Case where the stack is already empty
}
}
// Stack peek() method with arrays. Returns the integer at the top
// of the stack (the last of the list) or null if the stack is empty
public Integer peek(){
if(stack.length > 0){
return stack[stack.length-1];
} else{
return null;
}
}
}