package org.luzhen.test;
import java.util.LinkedList;
public class MainClass {
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());
}
}
}
class Queue {
private LinkedList list = new LinkedList();
public void put(Object v) {
//新元素放入链表首部 最后顺序是9,8,7,6,5,4,3,2,1,0
list.addFirst(v);
}
public Object get() {
//移除链表尾部元素
return list.removeLast();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
打印结果:
0
1
2
3
4
5
6
7
8
9
Process finished with exit code 0