휴지통/알고리즘 & 자료구조
Queue 구현
by 신재권
2021. 7. 15.
package review1;
import java.util.ArrayList;
public class QueueImplements<T> {
private ArrayList<T> queue = new ArrayList<T>();
//데이터 삽입
public void enqueue(T item){
queue.add(item);
}
//데이터 삭제
public T dequeue(){
if(queue.isEmpty()){
return null;
}
return queue.remove(0);
}
public boolean isEmpty(){
return queue.isEmpty();
}
public void printAll(){
System.out.print("[");
for(int i=0; i<queue.size();i++){
if(i == queue.size()-1)
System.out.print(queue.get(i));
else
System.out.print(queue.get(i)+", ");
}
System.out.println("]");
}
public T peek(){
return queue.get(0);
}
public static void main(String[] args) {
QueueImplements<Integer> queue = new QueueImplements<Integer>();
queue.enqueue(1);
queue.enqueue(5);
queue.enqueue(4);
queue.printAll();
System.out.println(queue.dequeue());
System.out.println(queue.peek());
queue.printAll();
}
}