Repository navigation
Expand file tree
/
Copy pathImprovedStackImpl.java
More file actions
90 lines (65 loc) · 1.7 KB
/
Copy pathImprovedStackImpl.java
File metadata and controls
90 lines (65 loc) · 1.7 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class ImprovedStackImpl implements ImprovedStack {
/** an implementation of ImprovedStack using ArrayList as the data structure
*/
private ArrayList internalList;
ReturnObject obj;
public ImprovedStackImpl(ArrayList list) {
this.internalList = list;
}
/**
* reverses the current stack; checks if stack is empty.
*/
public ImprovedStack reverse() {
ArrayList newList = new ArrayList();
ImprovedStack reverseStack = new ImprovedStackImpl(newList);
if (internalList.isEmpty()) {
obj = new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
obj.getError();
return reverseStack;
}
for (int i = 0; i < internalList.size(); i++) {
reverseStack.push(internalList.listArray[internalList.size()-1 - i]);
}
reverseStack.top();
return reverseStack;
}
/**
* removes all instances of the given value from the stack; checks if empty or null.
*/
public void remove(Object object) {
if (isEmpty()) {
obj = new ReturnObjectImpl(ErrorMessage.EMPTY_STRUCTURE);
obj.getError();
}
if (object == null) {
obj = new ReturnObjectImpl(ErrorMessage.INVALID_ARGUMENT);
} else {
for (int i = 0; i < internalList.size(); i++) {
if (internalList.listArray[i].equals(object)) {
internalList.remove(i);
i = 0;
} else {
}
}
}
}
public boolean isEmpty() {
return internalList.isEmpty();
}
public int size() {
return internalList.size();
}
public void push(Object item) {
internalList.add(item);
}
public ReturnObject top() {
obj = new ReturnObjectImpl(internalList.get(internalList.size()-1));
return obj;
}
public ReturnObject pop() {
obj = new ReturnObjectImpl(internalList.listArray[internalList.size()-1]);
obj.getReturnValue();
internalList.remove(internalList.size()-1);
return obj;
}
}