새소식

인기 검색어

비트교육_단기과정

원형 큐 실습

  • -
package week_5;
 
import java.util.Scanner;
 
public class BufferQueue {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        while (true) {
            System.err.println("1. 입력  2. 출력  3. 종료");
            int num = sc.nextInt();    
            if (num == 1) {    
                System.out.print("값을 입력 하시오 : ");    //넣을 값 입력 받기
                int input = sc.nextInt();
                Que.push(input);    
            } else if (num == 2) {    //출력
                int result = Que.pop();    //큐에서 값 꺼내기
                if(result != -1) {    //값이 존재할 경우 
                    System.out.println("pop : "+result);    //입력받은 숫자 출력
                }
            } else if (num == 3) {    //종료
                System.exit(0);
            }
        }
 
    }
 
    static class Que {    
        static int[] que = new int[5];    //큐의 사이즈
        static int last = 0;    
        static int first = 0;    
        static int count = 0;    
 
        static int pop() {    
            if(count == 0) {    
                System.out.println("Empty");    
                return -1;    
            }else {    
                if(first <5) {    
                    count--;    
                    return que[first++];    
                }else {    
                    first =0;    
                    count--;    
                    return que[first++];    
                }
            }
        }
 
        static void push(int num) {    
            if(count == 5) {    
                System.out.println("Full");    
            }else {    
                if(last<5) {    
                    que[last++= num;    
                    count++;    
                }else {    
                    last = 0;    
                    que[last++= num;    
                    count++;    
                }
            }
        }
    }
}
cs
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.