栈和队列的相互实现

1. 用两个栈实现一个队列

package como.queue;

import java.util.Stack;

/**
 * Description: 两个栈实现一个队列
 * Author: Administrator
 * Date: 2019/6/1
 * Time: 15:32
 */
public class TestMyQueue {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

2. 用两个队列实现一个栈

package com.queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Description: 两个队列实现一个栈
 * Author: Administrator
 * Date: 2019/5/28 0028
 * Time: 11:01
 */
public class TestMyStack {

    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    private int usedSize;

    /** Initialize your data structure here. */
    public TestMyStack() {
        this.queue1 = new LinkedList<>();
        this.queue2 = new LinkedList<>();
        this.usedSize = 0;
    }

    /** Push element x onto stack. */
    //入到不为空的队列
    public void push(int x) {
        if(!queue1.isEmpty()) {
            queue1.add(x);
        }else if(!queue2.isEmpty()) {
            queue2.add(x);
        }else {
            queue1.add(x);
        }
       this.usedSize++;
    }

    /** Removes the element on top of the stack and returns that element. */
    //从不为空的队列中删除
    public int pop() {
        int data = 0;
        if (!queue1.isEmpty()) {
            for (int i = 1; i <= usedSize-1; i++) {
                queue2.add(queue1.poll());
            }
            data = queue1.poll();
        }else {
            for (int i = 1; i <= usedSize-1; i++) {
                queue1.add(queue2.poll());
            }
            data = queue2.poll();
        }
        this.usedSize--;
        return data;
    }

    /** Get the top element. */
    public int top() {
        if (empty()) {
            throw new UnsupportedOperationException("无数据");
        }
        int data = 0;
        if (!queue1.isEmpty()) {
            for (int i = 0; i < this.usedSize; i++) {
                data = queue1.poll();
                queue2.add(data);
            }
        }else {
            for (int i = 0; i < this.usedSize; i++) {
                data = queue2.poll();
                queue1.add(data);
            }
        }
        return data;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return this.usedSize == 0;
    }
}

package com.queue;

public class TestDemo {

    public static void main(String[] args) {

        //两个队列实现一个栈,后进先出
        TestMyStack testMyStack = new TestMyStack();
        testMyStack.push(10);
        testMyStack.push(20);
        testMyStack.push(30);
        testMyStack.push(40);
        System.out.println(testMyStack.pop());//40
        testMyStack.push(50);
        System.out.println(testMyStack.pop());//50
        testMyStack.push(60);
        System.out.println(testMyStack.pop());//60
        System.out.println(testMyStack.pop());//30
        System.out.println(testMyStack.top());//20
        System.out.println(testMyStack.pop());//20
        System.out.println(testMyStack.pop());//10

        System.out.println("============");

        //两个栈实现一个队列,先进先出
        TestMyQueue testMyQueue = new TestMyQueue();
        testMyQueue.push(10);
        testMyQueue.push(20);
        testMyQueue.push(30);
        testMyQueue.push(40);
        System.out.println(testMyQueue.pop());//10
        testMyQueue.push(50);
        System.out.println(testMyQueue.pop());//20
        testMyQueue.push(60);
        System.out.println(testMyQueue.pop());//30
        System.out.println(testMyQueue.pop());//40
        System.out.println(testMyQueue.pop());//50
        System.out.println(testMyQueue.pop());//60

    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值