質問編集履歴

1

文の修正

2021/11/17 20:05

投稿

frere
frere

スコア4

test CHANGED
File without changes
test CHANGED
@@ -204,538 +204,4 @@
204
204
 
205
205
 
206
206
 
207
- キュー(線形リスト)
208
-
209
- package exer3.queue;
210
-
211
- import java.util.StringJoiner;
212
-
213
-
214
-
215
- public class LinkedListQueue<T> implements Queue<T> {
216
-
217
- private class Node {
218
-
219
- T data;
220
-
221
- Node next;
222
-
223
-
224
-
225
- Node(T d) {
226
-
227
- data = d;
228
-
229
- next = null;
230
-
231
- }
232
-
233
-
234
-
235
- void ins(Node n) {
236
-
237
- n.next = next;
238
-
239
- next = n;
240
-
241
- }
242
-
243
- }
244
-
245
-
246
-
247
- private Node in;
248
-
249
- private Node out;
250
-
251
-
252
-
253
- public LinkedListQueue(){
254
-
255
- clear();
256
-
257
- }
258
-
259
-
260
-
261
- public boolean enqueue(T d){
262
-
263
- in.ins(new Node(d));
264
-
265
- in = in.next;
266
-
267
- return true;
268
-
269
- }
270
-
271
-
272
-
273
- public T dequeue(){
274
-
275
- if (isEmpty())
276
-
277
- return null;
278
-
279
- if (out.next == in)
280
-
281
- in = out;
282
-
283
- T ans = out.next.data;
284
-
285
- out.next = out.next.next;
286
-
287
- return ans;
288
-
289
- }
290
-
291
-
292
-
293
- public boolean isEmpty() {
294
-
295
- return (in == out);
296
-
297
- }
298
-
299
-
300
-
301
- public void clear() {
302
-
303
- in = new Node(null);
304
-
305
- out = in;
306
-
307
- }
308
-
309
-
310
-
311
- public String toString() {
312
-
313
- StringJoiner j = new StringJoiner(",");
314
-
315
- for (Node p = out; null != p.next; p = p.next)
316
-
317
- j.add(p.next.data.toString());
318
-
319
- return " [" + j.toString() + "]";
320
-
321
- }
322
-
323
- }
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
- キュー(循環リスト)
332
-
333
- package exer3.queue;
334
-
335
-
336
-
337
- import java.util.StringJoiner;
338
-
339
- import exer3.linkedStructure.CircularlyLinkedList;
340
-
341
-
342
-
343
- public class CircularListQueue<T> implements Queue<T> {
344
-
345
- private CircularlyLinkedList<T> data;
346
-
347
-
348
-
349
- public CircularListQueue() {
350
-
351
- clear();
352
-
353
- }
354
-
355
-
356
-
357
- public boolean enqueue(T d) {
358
-
359
- data.insertZ(d);
360
-
361
- return true;
362
-
363
- }
364
-
365
- public T dequeue() {
366
-
367
- CircularlyLinkedList<T>.Node p = data.getFirst();
368
-
369
- T ans = data.get(p);
370
-
371
- data.delete(p);
372
-
373
- return ans;
374
-
375
- }
376
-
377
- public boolean isEmpty() {
378
-
379
- return data.isEmpty();
380
-
381
- }
382
-
383
-
384
-
385
- public void clear() {
386
-
387
- data = new CircularlyLinkedList<>();
388
-
389
- }
390
-
391
-
392
-
393
- public String toString() {
394
-
395
- if (isEmpty()) {
396
-
397
- return " []";
398
-
399
- } else {
400
-
401
- StringJoiner j = new StringJoiner(",");
402
-
403
- CircularlyLinkedList<T>.Node p = data.getFirst();
404
-
405
- do {
406
-
407
- j.add(data.get(p).toString());
408
-
409
- p = data.getNext(p);
410
-
411
- } while (! data.isFirst(p));
412
-
413
- return " [" + j.toString() + "]";
414
-
415
- }
416
-
417
- }
418
-
419
- }
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
- 線形リスト
428
-
429
- package exer3.linkedStructure;
430
-
431
- import java.util.StringJoiner;
432
-
433
-
434
-
435
- public class CircularlyLinkedList<T> {
436
-
437
- public class Node{
438
-
439
- T data;
440
-
441
- Node next;
442
-
443
- Node(){
444
-
445
- data = null;
446
-
447
- next = null;
448
-
449
- }
450
-
451
- Node(T d){
452
-
453
- this();
454
-
455
- data = d;
456
-
457
- }
458
-
459
- void ins(Node n){
460
-
461
- n.next = next;
462
-
463
- next = n;
464
-
465
- }
466
-
467
- }
468
-
469
- private Node tail;
470
-
471
-
472
-
473
- public CircularlyLinkedList() {
474
-
475
- tail = null;
476
-
477
- }
478
-
479
-
480
-
481
- public String toString() {
482
-
483
- Node p;
484
-
485
- StringJoiner j = new StringJoiner("\n");
486
-
487
- if (null != (p = tail)) {
488
-
489
- do {
490
-
491
- j.add(p.next.data.toString());
492
-
493
- } while (tail != (p = p.next));
494
-
495
- }
496
-
497
- j.add("-----");
498
-
499
- return j.toString();
500
-
501
- }
502
-
503
-
504
-
505
- public Node search(T d) {
506
-
507
- Node p;
508
-
509
- if (null != (p = tail)) {
510
-
511
- do {
512
-
513
- if (p.next.data.equals(d))
514
-
515
- return p;
516
-
517
- } while (tail != (p = p.next));
518
-
519
- }
520
-
521
- return null;
522
-
523
- }
524
-
525
-
526
-
527
- public CircularlyLinkedList<T> insertA(T d) {
528
-
529
- if (null == tail) {
530
-
531
- tail = new Node(d);
532
-
533
- tail.next = tail;
534
-
535
- } else
536
-
537
- tail.ins(new Node(d));
538
-
539
- return this;
540
-
541
- }
542
-
543
-
544
-
545
- public CircularlyLinkedList<T> insertP(T d, Node p) {
546
-
547
- if (null != p)
548
-
549
- p.ins(new Node(d));
550
-
551
- return this;
552
-
553
- }
554
-
555
-
556
-
557
- public CircularlyLinkedList<T> insertZ(T d) {
558
-
559
- insertA(d);
560
-
561
- tail = tail.next;
562
-
563
- return this;
564
-
565
- }
566
-
567
-
568
-
569
- public T get(Node p) {
570
-
571
- return (null == p ? null : p.next.data);
572
-
573
- }
574
-
575
-
576
-
577
- public CircularlyLinkedList<T> delete(Node p) {
578
-
579
- if (null != p) {
580
-
581
- if (p.next == p)
582
-
583
- tail = null;
584
-
585
- else {
586
-
587
- if (p.next == tail)
588
-
589
- tail = p;
590
-
591
- p.next = p.next.next;
592
-
593
- }
594
-
595
- }
596
-
597
- return this;
598
-
599
- }
600
-
601
-
602
-
603
- public Node getFirst() {
604
-
605
- return tail;
606
-
607
- }
608
-
609
-
610
-
611
- public Node getNext(Node p) {
612
-
613
- return (null == p ? null : p.next);
614
-
615
- }
616
-
617
-
618
-
619
- public boolean isFirst(Node p) {
620
-
621
- return (p == tail);
622
-
623
- }
624
-
625
-
626
-
627
- public boolean isEmpty() {
628
-
629
- return (null == tail);
630
-
631
- }
632
-
633
- }
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
- メインクラス
642
-
643
- package exer3.queue.tester;
644
-
645
- import exer3.queue.*;
646
-
647
- import java.io.*;
648
-
649
-
650
-
651
- public class Main {
652
-
653
- public static void main(String[] args) throws IOException {
654
-
655
- String line;
656
-
657
- Queue<String> q1, q2, q3;
658
-
659
- q1 = new FixedArrayQueue<>(3);
660
-
661
- q2 = new LinkedListQueue<>();
662
-
663
- q3 = new CircularListQueue<>();
664
-
665
-
666
-
667
- BufferedReader r =
668
-
669
- new BufferedReader(new InputStreamReader(System.in));
670
-
671
- while (null != (line = r.readLine())) {
672
-
673
- if (line.equals("d"))
674
-
675
- dequeueAction(q1, q2, q3);
676
-
677
- else
678
-
679
- enqueueAction(line, q1, q2, q3);
680
-
681
- }
682
-
683
- r.close();
684
-
685
- System.out.println("-----");
686
-
687
- while (!q3.isEmpty())
688
-
689
- dequeueAction(q1, q2, q3);
690
-
691
- }
692
-
693
-
694
-
695
- private static void enqueueAction(String line, Queue<String> a,
696
-
697
- Queue<String> b, Queue<String> c) {
698
-
699
-
700
-
701
- a.enqueue(line); System.out.println(a);
702
-
703
- b.enqueue(line); System.out.println(b);
704
-
705
- c.enqueue(line); System.out.println(c);
706
-
707
- }
708
-
709
-
710
-
711
- private static void dequeueAction(Queue<String> a, Queue<String> b,
712
-
713
-
714
-
715
- Queue<String> c) {
716
-
717
-
718
-
719
- String x = a.dequeue();
720
-
721
- String y = b.dequeue();
722
-
723
- String z = c.dequeue();
724
-
725
- if ((x != y) || (y != z))
726
-
727
- System.out.println("NG");
728
-
729
- else {
730
-
731
- System.out.print(x);
732
-
733
- System.out.println(a);
734
-
735
- }
736
-
737
- }
738
-
739
- }
740
-
741
207
  ```