teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

文の修正

2021/11/17 20:05

投稿

frere
frere

スコア4

title CHANGED
File without changes
body CHANGED
@@ -101,271 +101,4 @@
101
101
 
102
102
 
103
103
 
104
- キュー(線形リスト)
105
- package exer3.queue;
106
- import java.util.StringJoiner;
107
-
108
- public class LinkedListQueue<T> implements Queue<T> {
109
- private class Node {
110
- T data;
111
- Node next;
112
-
113
- Node(T d) {
114
- data = d;
115
- next = null;
116
- }
117
-
118
- void ins(Node n) {
119
- n.next = next;
120
- next = n;
121
- }
122
- }
123
-
124
- private Node in;
125
- private Node out;
126
-
127
- public LinkedListQueue(){
128
- clear();
129
- }
130
-
131
- public boolean enqueue(T d){
132
- in.ins(new Node(d));
133
- in = in.next;
134
- return true;
135
- }
136
-
137
- public T dequeue(){
138
- if (isEmpty())
139
- return null;
140
- if (out.next == in)
141
- in = out;
142
- T ans = out.next.data;
143
- out.next = out.next.next;
144
- return ans;
145
- }
146
-
147
- public boolean isEmpty() {
148
- return (in == out);
149
- }
150
-
151
- public void clear() {
152
- in = new Node(null);
153
- out = in;
154
- }
155
-
156
- public String toString() {
157
- StringJoiner j = new StringJoiner(",");
158
- for (Node p = out; null != p.next; p = p.next)
159
- j.add(p.next.data.toString());
160
- return " [" + j.toString() + "]";
161
- }
162
- }
163
-
164
-
165
-
166
- キュー(循環リスト)
167
- package exer3.queue;
168
-
169
- import java.util.StringJoiner;
170
- import exer3.linkedStructure.CircularlyLinkedList;
171
-
172
- public class CircularListQueue<T> implements Queue<T> {
173
- private CircularlyLinkedList<T> data;
174
-
175
- public CircularListQueue() {
176
- clear();
177
- }
178
-
179
- public boolean enqueue(T d) {
180
- data.insertZ(d);
181
- return true;
182
- }
183
- public T dequeue() {
184
- CircularlyLinkedList<T>.Node p = data.getFirst();
185
- T ans = data.get(p);
186
- data.delete(p);
187
- return ans;
188
- }
189
- public boolean isEmpty() {
190
- return data.isEmpty();
191
- }
192
-
193
- public void clear() {
194
- data = new CircularlyLinkedList<>();
195
- }
196
-
197
- public String toString() {
198
- if (isEmpty()) {
199
- return " []";
200
- } else {
201
- StringJoiner j = new StringJoiner(",");
202
- CircularlyLinkedList<T>.Node p = data.getFirst();
203
- do {
204
- j.add(data.get(p).toString());
205
- p = data.getNext(p);
206
- } while (! data.isFirst(p));
207
- return " [" + j.toString() + "]";
208
- }
209
- }
210
- }
211
-
212
-
213
-
214
- 線形リスト
215
- package exer3.linkedStructure;
216
- import java.util.StringJoiner;
217
-
218
- public class CircularlyLinkedList<T> {
219
- public class Node{
220
- T data;
221
- Node next;
222
- Node(){
223
- data = null;
224
- next = null;
225
- }
226
- Node(T d){
227
- this();
228
- data = d;
229
- }
230
- void ins(Node n){
231
- n.next = next;
232
- next = n;
233
- }
234
- }
235
- private Node tail;
236
-
237
- public CircularlyLinkedList() {
238
- tail = null;
239
- }
240
-
241
- public String toString() {
242
- Node p;
243
- StringJoiner j = new StringJoiner("\n");
244
- if (null != (p = tail)) {
245
- do {
246
- j.add(p.next.data.toString());
247
- } while (tail != (p = p.next));
248
- }
249
- j.add("-----");
250
- return j.toString();
251
- }
252
-
253
- public Node search(T d) {
254
- Node p;
255
- if (null != (p = tail)) {
256
- do {
257
- if (p.next.data.equals(d))
258
- return p;
259
- } while (tail != (p = p.next));
260
- }
261
- return null;
262
- }
263
-
264
- public CircularlyLinkedList<T> insertA(T d) {
265
- if (null == tail) {
266
- tail = new Node(d);
267
- tail.next = tail;
268
- } else
269
- tail.ins(new Node(d));
270
- return this;
271
- }
272
-
273
- public CircularlyLinkedList<T> insertP(T d, Node p) {
274
- if (null != p)
275
- p.ins(new Node(d));
276
- return this;
277
- }
278
-
279
- public CircularlyLinkedList<T> insertZ(T d) {
280
- insertA(d);
281
- tail = tail.next;
282
- return this;
283
- }
284
-
285
- public T get(Node p) {
286
- return (null == p ? null : p.next.data);
287
- }
288
-
289
- public CircularlyLinkedList<T> delete(Node p) {
290
- if (null != p) {
291
- if (p.next == p)
292
- tail = null;
293
- else {
294
- if (p.next == tail)
295
- tail = p;
296
- p.next = p.next.next;
297
- }
298
- }
299
- return this;
300
- }
301
-
302
- public Node getFirst() {
303
- return tail;
304
- }
305
-
306
- public Node getNext(Node p) {
307
- return (null == p ? null : p.next);
308
- }
309
-
310
- public boolean isFirst(Node p) {
311
- return (p == tail);
312
- }
313
-
314
- public boolean isEmpty() {
315
- return (null == tail);
316
- }
317
- }
318
-
319
-
320
-
321
- メインクラス
322
- package exer3.queue.tester;
323
- import exer3.queue.*;
324
- import java.io.*;
325
-
326
- public class Main {
327
- public static void main(String[] args) throws IOException {
328
- String line;
329
- Queue<String> q1, q2, q3;
330
- q1 = new FixedArrayQueue<>(3);
331
- q2 = new LinkedListQueue<>();
332
- q3 = new CircularListQueue<>();
333
-
334
- BufferedReader r =
335
- new BufferedReader(new InputStreamReader(System.in));
336
- while (null != (line = r.readLine())) {
337
- if (line.equals("d"))
338
- dequeueAction(q1, q2, q3);
339
- else
340
- enqueueAction(line, q1, q2, q3);
341
- }
342
- r.close();
343
- System.out.println("-----");
344
- while (!q3.isEmpty())
345
- dequeueAction(q1, q2, q3);
346
- }
347
-
348
- private static void enqueueAction(String line, Queue<String> a,
349
- Queue<String> b, Queue<String> c) {
350
-
351
- a.enqueue(line); System.out.println(a);
352
- b.enqueue(line); System.out.println(b);
353
- c.enqueue(line); System.out.println(c);
354
- }
355
-
356
- private static void dequeueAction(Queue<String> a, Queue<String> b,
357
-
358
- Queue<String> c) {
359
-
360
- String x = a.dequeue();
361
- String y = b.dequeue();
362
- String z = c.dequeue();
363
- if ((x != y) || (y != z))
364
- System.out.println("NG");
365
- else {
366
- System.out.print(x);
367
- System.out.println(a);
368
- }
369
- }
370
- }
371
104
  ```