-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
36 lines (27 loc) · 744 Bytes
/
ArrayStack.java
File metadata and controls
36 lines (27 loc) · 744 Bytes
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
import java.util.Arrays;
public class ArrayStack {
private int arr[];
private int top = -1;
//require an initial value constructor to know how large to make the stack
public ArrayStack(int capacity){
arr = new int[capacity];
}
//O(1) since the memory location of the element is known/computable
public void push(int data){
arr[++top] = data;
}
//O(1) since decrementing top
public int pop(){
return arr[top--];
}
//O(1) constant look up time for the top element in the stack
public int peek(){
return arr[top];
}
public Boolean isEmpty(){
return top == -1;
}
public String print(){
return Arrays.toString(arr);
}
}