package queue;
public class QueueTest {
public static void main(String[] args) {
Queue a=new Queue();
try{
a.add(12);
a.add(13);
// a.add(14);
// System.out.println(a.take());
System.out.println(a.take());
System.out.println(a.take());
// System.out.println(a.take());
}
catch(IsEmptyException e){
System.err.println(e.getMessage());
}
catch(IsFullException e){
System.err.println(e.getMessage());
}
}
}
package queue;
public class Queue {
private final int Max_size = 3;
private int head = 0;
private int tail = 0;
private int[] q = new int[Max_size];
public boolean isFull() {
return (tail + 1) % Max_size == head;
}
public boolean isEmpty() {
return tail == head;
}
public void add(int x) throws IsFullException {
if (isFull()) {
throw new IsFullException("Queue is Full");
} else {
q[tail] = x;
tail = (tail + 1) % Max_size;
}
}
public int take() throws IsEmptyException {
if (isEmpty()) {
throw new IsEmptyException("Queue is Empty");
} else {
int x;
x = q[head];
head = (head + 1) % Max_size;
return x;
}
}
package queue;
public class IsEmptyException extends Exception {
public IsEmptyException(String message){
super(message);
}
}
package queue;
public class IsFullException extends Exception {
public IsFullException(String message){
super(message);
}
}
:: موضوعات مرتبط:
ساختمان داده ها ,
,
:: برچسبها:
صف ,
آرایه ,
جاوا ,
کلاس ,
:: بازدید از این مطلب : 425
|
امتیاز مطلب : 0
|
تعداد امتیازدهندگان : 0
|
مجموع امتیاز : 0