728x90
interface Collection{
int MAX = 100;
void add(Object obj);
void delete(Object obj);
Object find(Object obj);
int currentCount();
}
class Queue implements Collection{
private Object[] queue = new Object[MAX];
private int rear = -1;
private int front = 0;
private int count = 0;
public void add(Object obj) {
queue[++rear] = obj;
count++;
}
public void delete(Object obj) {
for(int i=front; i<rear; i++) {
if(queue[i]==obj)
queue[i]=null;
}
front++;
count--;
}
public Object find(Object obj) {
for(int i=front; i<rear; i++) {
if(queue[i]==obj) {
return i;
}
}
return null;
}
public int currentCount() {
return count;
}
}
public class chp6_9 {
public static void main(String[] args) {
Queue q = new Queue();
q.add(3);
q.add(4);
q.add(5);
System.out.println(q.find(4));
System.out.println(q.currentCount());
q.delete(4);
System.out.println(q.find(4));
System.out.println(q.currentCount());
}
}
반응형