-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathStack.java
More file actions
35 lines (27 loc) · 811 Bytes
/
Stack.java
File metadata and controls
35 lines (27 loc) · 811 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
package StackArrayList;
import jdk.nashorn.internal.runtime.regexp.joni.constants.StackType;
import java.util.ArrayList;
/**
* Implement Stack<E> by adding the push, pop, and isEmpty functions. It must pass the prewritten unit tests.
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList<E> elements;
public Stack(){
this.elements = new ArrayList<>();
}
public void push(E element){
elements.add(element);
}
public E pop() throws IndexOutOfBoundsException{
E result = elements.get(elements.size()-1);
elements.remove(result);
return result;
}
public boolean isEmpty(){
if (elements.size() == 0){
return true;
}
return false;
}
}