1 건의
큐 검색결과
  1. 2018.02.11 KOREAPAS



public class Q {


private int head;

private int tail;

private int size;

private Integer[] q;


public Q(int size) {

q = new Integer[size];

head = 0;

tail = 0;

}// Q.constructor


public void print() {

System.out.print("[ ");

if (head < tail) {

for (int i = head; i < tail; i++) {

System.out.printf("%d", q[i]);

if (q[i + 1] != null)

System.out.print(", ");

}

System.out.println(" ] ");

} else {

for (int i = head; i < q.length - 1; i++) {

System.out.printf("%d, ", q[i]);

}

System.out.printf("%d", q[q.length - 1]);

if (q[0] != null)

System.out.print(", ");

for (int i = 0; i < tail; i++) {

if (q[i] != null) {

System.out.printf("%d", q[i]);

if (i != tail - 1)

System.out.print(", ");

}

}

System.out.println(" ] ");

}

}// Q.print


public void enqueue(int a) {

if ( isFull() ) {

System.out.println("this queue is full. you can not enqueue anymore.");

return;

}

q[tail] = a;

if (tail == q.length - 1)

tail = 0;

else

tail++;

print();


}// Q.enqueue


public void dequeue() {

if( !isFull() && head == tail ) {

System.out.println("this queue is empty. you can not dequeue anymore.");

return;

}

q[head] = null;

if (head == q.length - 1)

head = 0;

else

head++;

print();

}// Q.dequeue

public boolean isFull() {

if(head == tail && q[head] != null)

return true;

else

return false;

}// Q.isFull

}// class Q