-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImprovedStackImpl.java
56 lines (47 loc) · 1.25 KB
/
ImprovedStackImpl.java
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
46
47
48
49
50
51
52
53
54
55
56
public class ImprovedStackImpl implements ImprovedStack {
protected List list;
public ImprovedStackImpl(List list) {
this.list = list;
}
public boolean isEmpty() {
return list.isEmpty();
}
public int size() {
return list.size();
}
//top of the list is the same as the first item in the list - LIFO
public void push(Object item) {
list.add(0,item);
}
public ReturnObject top() {
if (isEmpty()) {
return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE);
}
return list.get(0);
}
public ReturnObject pop() {
if (isEmpty()) {
return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE);
}
return list.remove(0);
}
public ImprovedStack reverse() {
if (isEmpty()) {
return this;
}
List listCopy = new ArrayList();
for (int i = list.size() - 1; i >= 0; i--) { //iterating backwards through list
listCopy.add(list.get(i).getReturnValue()); //adding list's last element to the beginning of copy etc
}
return new ImprovedStackImpl(listCopy);
}
public void remove(Object object) {
List listCopy = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).getReturnValue().equals(object)) {
listCopy.add(list.get(i).getReturnValue());
}
}
list = listCopy;
}
}