-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathStack.java
More file actions
45 lines (29 loc) · 860 Bytes
/
Stack.java
File metadata and controls
45 lines (29 loc) · 860 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
37
38
39
40
41
42
43
44
45
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> {
//DONT FORGET TO INITIALIZE TO AVOID NULL POINTER EXCEPTIONS!!!
private ArrayList <E> elements = new ArrayList<>();
public Stack(){
}
public Stack(ArrayList elements) {
this.elements = elements;
}
public void push(E item){
elements.add(item);
}
public E pop(){
E last = elements.get(elements.size()-1);
elements.remove(elements.size()-1);
return last;
}
public boolean isEmpty(){
if(elements.size() == 0) {
return true;
}else
return false;
}
}