-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.java
More file actions
201 lines (167 loc) · 5.84 KB
/
app.java
File metadata and controls
201 lines (167 loc) · 5.84 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Данный программный код написан в учебных целях.
*/
import java.util.Optional;
class App {
public static void main(String[] args) {
new MyStack<Integer>().min();
MyStack<Integer> stack = new MyStack<Integer>();
stack.push(20);
stack.push(5);
stack.push(30);
stack.push(0);
System.out.println("Test pop: " + stack.pop().get());
System.out.println("Test pop: " + stack.pop().get());
//System.out.println("Test peek: " + stack.peek().get());
System.out.println("Max: " + stack.max().get());
System.out.println("Min: " + stack.min().get());
}
}
interface Stack<T> {
public void push(T obj);
public Optional<T> pop();
public Optional<T> peek();
public Optional<T> max();
public Optional<T> min();
}
class MyStack<T extends Comparable<T>> implements Stack<T> {
private Item<T> top;
private ItemElement element;
private ItemMax max;
private ItemMin min;
private abstract class Item<T extends Comparable<T>> implements Comparable<Item<T>> {
private T currentItem;
private Item<T> prevItem;
public Item(T currentItem, Item<T> prevItem) {
this.currentItem = currentItem;
this.prevItem = prevItem;
}
protected T peek() {
return this.currentItem;
}
protected Item<T> getPrev() {
return this.prevItem;
}
protected boolean first() {
return this.prevItem == null;
}
protected ItemMax getPrevMax() {
return null;
}
protected ItemMin getPrevMin() {
return null;
}
@Override
public int compareTo(Item<T> item) {
int result = this.peek().compareTo(item.peek());
return result;
}
}
private class ItemMax extends Item<T> {
private ItemMax prevMax;
private ItemMax(Item<T> item) {
super(item.currentItem, item.prevItem);
}
private ItemMax(Item<T> item, ItemMax prevMax) {
super(item.currentItem, item.prevItem);
this.prevMax = prevMax;
}
private ItemElement getItem() {
return new ItemElement(super.currentItem, super.prevItem);
}
@Override
protected ItemMax getPrevMax() {
return this.prevMax;
}
}
private class ItemMin extends Item<T> {
private ItemMin prevMin;
private ItemMin(Item<T> item) {
super(item.currentItem, item.prevItem);
}
private ItemMin(Item<T> item, ItemMin prevMin) {
super(item.currentItem, item.prevItem);
this.prevMin = prevMin;
}
@Override
protected ItemMin getPrevMin() {
return this.prevMin;
}
}
private class ItemElement extends Item<T> {
private ItemElement(T currentItem, Item<T> prevItem) {
super(currentItem, prevItem);
}
protected ItemMax max(ItemMax x) {
if (x == null) x = new ItemMax(this);
if (x.compareTo(this) > 0) {
return x;
} else {
return new ItemMax(this, x);
}
}
protected ItemMin min(ItemMin x) {
if (x == null) x = new ItemMin(this);
if (x.compareTo(this) < 0) {
return x;
} else {
return new ItemMin(this, x);
}
}
}
public MyStack() {
}
/* Вносит объект в стек */
public void push(T obj) {
this.element = new ItemElement(obj, this.top);
this.max = this.element.max(this.max);
this.min = this.element.min(this.min);
if (this.top == null) this.top = this.element;
else if (this.max.peek() == this.element.peek()) this.top = this.max;
else if (this.min.peek() == this.element.peek()) this.top = this.min;
}
/* Возвращает ссылку на верхний объект стека с последующим удалением объекта из стека */
public Optional<T> pop() {
Optional<Item<T>> obj = Optional.ofNullable(this.top);
Optional<T> result = Optional.empty();
if (obj.isPresent()) {
Item<T> item = obj.get();
result = Optional.ofNullable(item.peek());
if (!item.first()) {
if (item.getPrevMax() != null) {
this.max = item.getPrevMax();
}
if (item.getPrevMin() != null) {
this.min = item.getPrevMin();
}
this.top = item.getPrev();
} else {
this.top = null;
this.min = null;
this.max = null;
}
}
return result;
}
/* Возвращает ссылку на верхний объект стека */
public Optional<T> peek() {
Optional<Item<T>> optional = Optional.ofNullable(this.top);
if(optional.isPresent())
return Optional.ofNullable(optional.get().peek());
return Optional.empty();
}
/* Возвращает ссылку на максимальный объект в стеке */
public Optional<T> max() {
Optional<Item<T>> optional = Optional.ofNullable(this.max);
if(optional.isPresent())
return Optional.ofNullable(optional.get().peek());
return Optional.empty();
}
/* Возвращает ссылку на минимальный объект в стеке */
public Optional<T> min() {
Optional<Item<T>> optional = Optional.ofNullable(this.min);
if(optional.isPresent())
return Optional.ofNullable(optional.get().peek());
return Optional.empty();
}
}