-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteque.java
198 lines (161 loc) · 5.84 KB
/
teque.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
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
import java.util.*;
import java.io.*;
public class teque {
public static void main(String[] args) {
Kattio io = new Kattio (System.in, System.out);
int numOfOperations = io.getInt();
QueueArr frontTeque = new QueueArr();
QueueArr backTeque = new QueueArr();
for (int i=0; i<numOfOperations;i++){
String operationGiven = io.getWord();
Integer numberInput = io.getInt();
if (operationGiven.equals("push_back")){
backTeque.push_back(numberInput);
if (backTeque.size - frontTeque.size > 0){
frontTeque.push_back(backTeque.get(0));
backTeque.removeFront();
}
}
else if (operationGiven.equals("push_front")){
frontTeque.push_front(numberInput);
if (frontTeque.size - backTeque.size > 1){
backTeque.push_front(frontTeque.get(frontTeque.size-1));
frontTeque.removeBack();
}
}
else if (operationGiven.equals("push_middle")){
//frontTeque shorter than backTeque, we push to the back of the frontTeque
if (frontTeque.size <= backTeque.size){
frontTeque.push_back(numberInput);
}
//if frontTeque longer than backTeque, we push to the front of the backTeque
else{
backTeque.push_front(numberInput);
}
}
else if (operationGiven.equals("get")){
//empty teque capacity is always at least 2
if (numberInput< frontTeque.size){
io.println(frontTeque.get(numberInput));
} else {
numberInput = numberInput-frontTeque.size;
io.println(backTeque.get(numberInput));
}
}
}
io.close();
}
}
class QueueArr{
public int[] arr;
public int front, back;
public int capacity;
public final int INITSIZE = 1;
public int size;
public QueueArr() {
arr = new int[INITSIZE]; // create array of integers
front = 0; // the queue is empty
back = 0;
size = 0;
capacity = INITSIZE;
}
public boolean empty() {
return (front == back);
}
public Integer get(int index) {
if (empty()) return null;
else {
//calculate the actual index of the element based on the front pointer and the input index using the modulo operator.
//Finally, you return the element at the calculated index. CIRCULAR ARRAY...
int actualIndex = (front + index) % arr.length;
return arr[actualIndex];
}
}
public void push_front(Integer item) {
if (((back+1)%capacity) == front){ // one space left means array is full, we are enlarging it
enlargeArr();
}
//use the modulo function and create your own formula to wrap around the array to maintain the circular queue
front = (front - 1 + capacity) % capacity;
arr[front] = item;
size++;
}
public void push_back(Integer item) {
if (((back+1)%capacity) == front){ // array is full
enlargeArr();
}
//lecture given way to maintain circular queue (so it doesnt go out of bounds)
arr[back] = item;
back = (back + 1) % capacity;
size++;
}
public void removeFront(){
front = (front + 1) % capacity;
size--;
}
public void removeBack(){
back = (back - 1 + capacity) % capacity;
size--;
}
public boolean enlargeArr() {
int newSize = capacity * 2;
int[] temp = new int[newSize];
for (int j=0; j < capacity; j++) {
// copy the front (1st) element, 2nd element, ..., in the
// original array to the 1st (index 0), 2nd (index 1), ...,
// position in the enlarged array
temp[j] = arr[(front+j) % capacity];
}
front = 0;
back = capacity - 1;
arr = temp;
capacity = newSize;
return true;
}
}
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}