jdk中原本是个filo队列,现在我改了一下源码,成为一个fifo的。
import java.util.*;
public
class Stack extends Vector {
public Stack() {
}
public Object push(Object item) {
addElement(item);
return item;
}
public synchronized Object pop() {
Object obj;
int len = size();
obj = peek();
removeElementAt(0);
return obj;
}
public synchronized Object peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(0);
}
public boolean empty() {
return size() == 0;
}
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
private static final long serialVersionUID = 1224463164541339165L;
}
//: c09:Queue.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Making a queue from a LinkedList.
import java.util.*;
public class 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();
}
public static void main(String[] args) {
Queue queue = new Queue();
for(int i = 0; i < 10; i++)
queue.put(Integer.toString(i));
while(!queue.isEmpty())
System.out.println(queue.get());
}
} ///
浙公网安备 33010602011771号