java 使用两个栈实现队列

来源:互联网 发布:apple解压软件 编辑:程序博客网 时间:2024/06/12 19:18
public class StackToQueue {    private static final String TAG  ="StackToQueue";    public Stack<TestBean> fStack = new Stack<>();    public Stack<TestBean> sStack = new Stack<>();    public void insert(TestBean bean){        fStack.push(bean);    }    public TestBean delete(){        //just print data        for (int z = 0; z < fStack.size(); z++) {            //System.out.println(TAG+"--print current fStack--"+fStack.get(z).name);        }        //clear sStack        for (int i = 0; i < sStack.size(); i++) {            sStack.pop();        }        //put data to sStack        int sizeOrigin = fStack.size();        for (int i = 0; i < sizeOrigin; i++) {            sStack.push(fStack.pop());        }        //just print data        for (int z = 0; z < sStack.size(); z++) {            //System.out.println(TAG+"--to sStack--"+sStack.get(z).name);        }        TestBean t = sStack.pop();        int currentSize = sStack.size();        for (int i = 0; i < currentSize; i++) {            fStack.push(sStack.pop());        }        //just print data        for (int z = 0; z < fStack.size(); z++) {            //System.out.println(TAG+"--to fStack--"+fStack.get(z).name);        }        return t;    }    public static class TestBean{        public TestBean(String name) {            this.name = name;        }        String name;    }    public static void test(){        StackToQueue s = new StackToQueue();        for (int i = 0; i < 4; i++) {            s.insert(new TestBean(TAG+"index="+i));        }        System.out.println(TAG+"======check sStack====");        for (int i = 0; i < 4; i++) {            System.out.println(TAG+"=="+s.fStack.get(i).name);        }        for (int i = 0; i < 4; i++) {            System.out.println(TAG+"======delete====");            System.out.println(TAG+"=s.delete().name="+s.delete().name);            for (int z = 0; z < s.fStack.size(); z++) {                System.out.println(TAG+"=="+s.fStack.get(z).name);            }        }    }}

=====result=====


12-05 14:41:20.870 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue======check sStack====12-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=012-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=112-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=212-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=312-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue======delete====12-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue=s.delete().name=StackToQueueindex=012-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=112-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=212-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=312-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue======delete====12-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue=s.delete().name=StackToQueueindex=112-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=212-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=312-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue======delete====12-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue=s.delete().name=StackToQueueindex=212-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue==StackToQueueindex=312-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue======delete====12-05 14:41:20.871 2727-2727/com.example.bxh.sayhello I/System.out: StackToQueue=s.delete().name=StackToQueueindex=3

==============

可以想象一下,有两个“直径比乒乓球略大”的杯子A&B,向A中放进去四个乒乓球,最底下的是0号球,最上面的是3号球,这时候希望取出最底下的乒乓球,即0号球,此时将A中的球“一个一个”地放到B中,放完后,B中最上面的球就是0号球,将其取出来以后,再把B中的球一个一个的放进A中,此时A中最底下的球是1号球,最上面的是3号球。至此,达成取出0号球的目的。so,插入&删除 就是此过程的往复。



0 0
原创粉丝点击