package com.ijiami.main.shiro.test; import java.util.ArrayList; import java.util.List; /** * * @author Administrator * list 实现栈、队列 * */ public class QueueAndStack { int size = 0; List list = new ArrayList(); public void putList(Object o){ this.list.add(o); size = list.size(); } public Object getStack(){ List stack = new ArrayList(); Object o = list.get(list.size()-1); for (int i = 0; i < list.size()-1; i++) { stack.add(list.get(i)); } list = stack; size = list.size(); return o; } public Object getQueue(){ List queue = new ArrayList(); Object o = list.get(0); for (int i = 1; i <= list.size()-1; i++) { queue.add(list.get(i)); } list = queue; size = list.size(); return o; } public static void main(String[] args) { QueueAndStack heapAndStack = new QueueAndStack(); heapAndStack.putList("1"); heapAndStack.putList("2"); heapAndStack.putList("3"); int length = heapAndStack.size; for (int i = 0; i < length; i++) { System.out.println(heapAndStack.getQueue()); } System.out.println("----"); heapAndStack.putList("4"); heapAndStack.putList("5"); length = heapAndStack.size; for (int i = 0; i < length; i++) { System.out.println(heapAndStack.getQueue()); } System.out.println("----"); QueueAndStack heapAndStack0 = new QueueAndStack(); heapAndStack0.putList("1"); heapAndStack0.putList("2"); heapAndStack0.putList("3"); length = heapAndStack0.size; for (int i = 0; i < length; i++) { System.out.println(heapAndStack0.getStack()); } System.out.println("----"); heapAndStack0.putList("4"); heapAndStack0.putList("5"); length = heapAndStack0.size; for (int i = 0; i < length; i++) { System.out.println(heapAndStack0.getStack()); } } }