-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathStack.java
More file actions
33 lines (24 loc) · 763 Bytes
/
Stack.java
File metadata and controls
33 lines (24 loc) · 763 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
package StackArrayList;
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 = new ArrayList<>();
public Stack(){
}
public void push(E input) throws IndexOutOfBoundsException{
elements.add(input);
}
public E pop() throws IndexOutOfBoundsException{
int last = elements.size()-1;
E top= elements.get(elements.size()-1);
elements.remove(last);
return top;
}
public boolean isEmpty() {
if (elements.size() == 0) return true;
else return false;
}
}