小灰算法--用栈实现队列

       两个栈实现队列:

import java.util.Stack;

public class stackToqueue {


    private Stack<Integer> stackA = new Stack<Integer>();
    private Stack<Integer> stackB = new Stack<Integer>();

    public void enQueue(int element){
        stackA.push(element);
    }
    public Integer deQueue(){
        if(stackB.isEmpty()){
            if(stackA.isEmpty()){
                return null;
            }
            transfer();
        }
        return stackB.pop();
    }
    private void transfer(){
        while(!stackA.isEmpty()){
            stackB.push(stackA.pop());
        }
    }

    public static void main(String[] args) throws Exception{
        stackToqueue stackQueue = new stackToqueue();
        stackQueue.enQueue(1);
        stackQueue.enQueue(2);
        stackQueue.enQueue(3);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
        stackQueue.enQueue(4);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
    }
}

       两个队列实现栈:


import java.util.LinkedList;

public class QueuesToStack
{
    LinkedList<Integer> queue1=new LinkedList<Integer>();
    LinkedList<Integer> queue2=new LinkedList<Integer>();
    public void push(int value)//入栈
    {
        queue1.addLast(value);

    }

    public int pop()//出栈     必须是非空的栈才能出栈啊
    {
        if(sSize()!=0)//栈不为空
        {
            //移动一个队的n-1个到另一个中
            if(!queue1.isEmpty())//q1 空
            {
                putN_1ToAnthor();
                return queue1.removeFirst();
            }
            else  //q2 空
            {
                putN_1ToAnthor();
                return queue2.removeFirst();
            }
        }
        else
        {
            System.out.println("栈已经为空啦,不能出栈");
            return -1;
        }

    }

    public int sSize()
    {
        return queue1.size()+queue2.size();
    }

    public void putN_1ToAnthor()//从非空中出队n-1个到另一个队列   因为队列总是一空一非空
    {
        if(!queue1.isEmpty())
        {
            while(queue1.size()>1)
            {
                queue2.addLast(queue1.removeFirst());
            }
        }
        else if(!queue2.isEmpty())
        {
            while(queue2.size()>1)
            {
                queue1.addLast(queue2.removeFirst());
            }
        }
    }
    public static void main(String[] args)
    {
        QueuesToStack stack=new QueuesToStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        stack.push(5);
        stack.push(6);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值