신규 블로그를 만들었습니다!

2020년 이후부터는 아래 블로그에서 활동합니다.

댓글로 질문 주셔도 확인하기 어려울 수 있습니다.

>> https://bluemiv.tistory.com/
package queue;
 
public class CirculationQueue {
 
    private final int SIZE = 10;
    private Object[] buffer = new Object[SIZE];
    private int tail = 0, head = 0;
 
    public Object nextIndex(int index) {
        return (index+1)%SIZE;
    }
 
    public void push(Object item) {
        if ((tail + 1) % SIZE == head % SIZE) {
            System.out.println("Queue is Full! ["+item+" can't push]");
        } else {
            buffer[tail] = item;
            tail = (Integer)nextIndex(tail);
        }
    }
 
    public Object pop() {
        if (tail % SIZE == head % SIZE) {
            System.out.println("Queue is Empty!");
            return null;
        } else {
            Object result = buffer[head]; 
            head = (Integer)nextIndex(head);
            return result;
        }
    }
 
    public static void main(String[] args) {
        CirculationQueue queue = new CirculationQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.push(5);
        queue.push(6);
        queue.push(7);
        queue.push(8);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        queue.push(9);
        queue.push(10);
        queue.push(11);
        queue.push(12);
        queue.push(13);
        queue.push(14);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }
}​

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기