JAva一般キューとループキューコード実装


キュー
アレイシミュレーション通常キュー
package com.queue;

import java.util.Scanner;

public class ArrayQueueDemo {
     
    public static void main(String[] args) {
     
        //  
        //      
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' '; //      
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //      
        while(loop){
     
            System.out.println("s(show):    ");
            System.out.println("e(exit):    ");
            System.out.println("a(add):       ");
            System.out.println("g(get):    ");
            System.out.println("h(head):       ");
            key = scanner.next().charAt(0); //      
            switch (key) {
     
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("      ");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g'://    
                    try {
     
                        int res = queue.getQueue();
                        System.out.printf("      %d
"
, res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': // try { int res = queue.headQueue(); System.out.printf(" %d
"
, res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e':// scanner.close(); loop = false; break; default: break; } } System.out.println(" --"); } } // , ArrayQueue class ArrayQueue { private int maxSize; // private int front; // private int rear;// private int[] arr;// // public ArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; front = -1; // rear = -1; // , } // public boolean isFull() { return rear == maxSize - 1; } // public boolean isEmpty() { return rear == front; } // public void addQueue(int n) { // if (isFull()) { System.out.println(" "); return; } rear++; //rear arr[rear] = n; } // public int getQueue(){ // if (isEmpty()){ // throw new RuntimeException(" "); } front++; // front return arr[front]; } // public void showQueue(){ // if (isEmpty()){ System.out.println(" "); return; } for (int i = 0; i < arr.length; i++) { System.out.printf("arr[%d]=%d
"
,i,arr[i]); } } // , public int headQueue(){ // if (isEmpty()){ throw new RuntimeException(" "); } return arr[front+1]; } }

アレイシミュレーションリングキュー
package com.queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {
     
    public static void main(String[] args) {
     
        //  
        //      
        CircleQueue queue = new CircleQueue(4); //   4,     3,arr[3]  
        char key = ' '; //      
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //      
        while (loop) {
     
            System.out.println("s(show):    ");
            System.out.println("e(exit):    ");
            System.out.println("a(add):       ");
            System.out.println("g(get):    ");
            System.out.println("h(head):       ");
            key = scanner.next().charAt(0); //      
            switch (key) {
     
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("      ");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g'://    
                    try {
     
                        int res = queue.getQueue();
                        System.out.printf("      %d
"
, res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': // try { int res = queue.headQueue(); System.out.printf(" %d
"
, res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e':// scanner.close(); loop = false; break; default: break; } } System.out.println(" --"); } } // , ArrayQueue class CircleQueue { private int maxSize; // private int front; // private int rear;// private int[] arr;// // public CircleQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; front = 0; // : rear = 0; // : } // public boolean isFull() { return (rear+1)%maxSize == front; } // public boolean isEmpty() { return rear == front; } // public void addQueue(int n) { // if (isFull()) { System.out.println(" "); return; } arr[rear] = n; rear=(rear+1)%maxSize; //rear } // public int getQueue(){ // if (isEmpty()){ // throw new RuntimeException(" "); } //front // front , , int value =arr[front]; front =(front+1)%maxSize; return value; } // public void showQueue(){ // if (isEmpty()){ System.out.println(" "); return; } // front , for (int i = front; i < front+size(); i++) { System.out.printf("arr[%d]=%d
"
,i%maxSize,arr[i%maxSize]); } } // public int size(){ return (rear+maxSize-front)%maxSize; } // , public int headQueue(){ // if (isEmpty()){ throw new RuntimeException(" "); } return arr[front]; } }

个人学习ノート、最后に见た大人にほめてもらいましょう(#^.^#)!!