1. 程式人生 > >佇列和堆疊 --- 先進先出和先進後出

佇列和堆疊 --- 先進先出和先進後出

private static void queue()//  佇列
{
Queue<String> queue  = new LinkedList<>();
queue.offer("Hello");
queue.offer("world");
queue.offer("I m find");
System.out.println("--------------------------");
System.out.println(queue.size());
String aa ;
while((aa = queue.poll()) !=null)
{
System.out.println("queue---"+aa);
}
System.out.println(queue.size());
System.out.println("--------------------------");
}


private static void stack() // 堆疊
{
Vector<String> stack = new Vector<String>();
stack.add("aaa");
stack.add("bbb");
stack.add("ccc");
stack.add("ddd");
stack.add("fff");
System.out.println("---------------------------");
System.out.println("size"+stack.size());

String aa ;
try {
while((aa = stack.lastElement()) !=null)
{
System.out.println("a---"+aa);
stack.removeElement(aa);
//break;
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("size"+stack.size());

}

佇列和堆疊區別:--- 先進先出和先進後出

System:

--------------------------
3
queue---Hello
queue---world
queue---I m find
0
--------------------------
---------------------------
size5
a---fff
a---ddd
a---ccc
a---bbb
a---aaa
size0