java代码实现队列
程序员文章站
2024-03-01 13:12:46
...
java代码实现队列
单向队列:
import java.util.LinkedList;
//单向队列
public class Queue {
public Queue() {
}
private LinkedList list = new LinkedList();
public void put(Object v) {
list.addFirst(v);
}
public Object get() {
return list.removeLast();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
双向队列:
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
// 双向队列
public class DQueue implements Collection {
private LinkedList lists;
public DQueue() {
lists = new LinkedList();
}
public void put_front(Object v) {
lists.addFirst(v);
}
public void put_back(Object v) {
lists.addLast(v);
}
public Object get_front() {
return lists.removeFirst();
}
public Object get_Back() {
return lists.removeLast();
}
public boolean isEmpty() {
return lists.isEmpty();
}
public int size() {
return lists.size();
}
public boolean contains(Object o) {
return lists.contains(o);
}
public Iterator iterator() {
return lists.iterator();
}
public Object[] toArray() {
return lists.toArray();
}
public Object[] toArray(Object a[]) {
return lists.toArray(a);
}
public boolean add(Object o) {
lists.addLast(o);
return true;
}
public boolean remove(Object o) {
return lists.remove(o);
}
public boolean containsAll(Collection c) {
return lists.containsAll(c);
}
public boolean addAll(Collection c) {
return lists.addAll(c);
}
public boolean removeAll(Collection c) {
return lists.removeAll(c);
}
public boolean retainAll(Collection c) {
return lists.retainAll(c);
}
public void clear() {
lists.clear();
}
}