面试/算法--使用两个栈来实现队列--java

使用两个栈来实现队列

Queue

import java.util.AbstractQueue;
import java.util.Iterator;
import java.util.Stack;

/**
 * Created by admin on 2017/3/6.
 */
public class MyQueue<E> extends AbstractQueue<E> {

    private Stack s1;
    private Stack s2;

    public MyQueue() {
        s1 = new Stack();
        s2 = new Stack();
    }

    @Override
    public Iterator iterator() {
        throw new RuntimeException("not support!");
    }

    @Override
    public int size() {
        return s1.size()+s2.size();
    }

    @Override
    public boolean offer(E e) {
        s1.push(e);
        return true;
    }

    @Override
    public E poll() {
        if(s2.isEmpty()){
            while(!s1.isEmpty()){
                s2.push(s1.pop());
            }
        }
        if(s2.isEmpty()){
            return null;
        }

        return s2.pop();
    }

    @Override
    public E peek() {
        if(s2.isEmpty()){
            while(!s1.isEmpty()){
                s2.push(s1.pop());
            }
        }
        if(s2.isEmpty()){
            return null;
        }
        return s2.peek();
    }

    @Override
    public String toString() {
        return "MyQueue{" + "s1=" + s1 + ", s2=" + s2 + '}';
    }
}

demo

public static void main(String[] args){

            MyQueue queue = new MyQueue();

            for (int i = 0; i < 10; i++) {
                queue.offer(i);

                if(i==5){
                    queue.poll();
                }
            }

            while(!queue.isEmpty()){
                System.out.println(queue);
                System.out.println(queue.poll());
            }
            System.out.println(queue.poll());

        }

输出

MyQueue{s1=[6, 7, 8, 9], s2=[5, 4, 3, 2, 1]}
1
MyQueue{s1=[6, 7, 8, 9], s2=[5, 4, 3, 2]}
2
MyQueue{s1=[6, 7, 8, 9], s2=[5, 4, 3]}
3
MyQueue{s1=[6, 7, 8, 9], s2=[5, 4]}
4
MyQueue{s1=[6, 7, 8, 9], s2=[5]}
5
MyQueue{s1=[6, 7, 8, 9], s2=[]}
6
MyQueue{s1=[], s2=[9, 8, 7]}
7
MyQueue{s1=[], s2=[9, 8]}
8
MyQueue{s1=[], s2=[9]}
9
null

拓展-使用两个队列来实现栈

Stack

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

/**
 * Created by admin on 2017/3/6.
 */
public class MyStack {

    private Queue q1;
    private Queue q2;

    public MyStack() {
        q1 = new LinkedList();
        q2 = new LinkedList();
    }

    public boolean isEmpty(){
        return q1.size()+q2.size()==0;
    }

    public E peek(){
        if(!q1.isEmpty()){
            return q1.peek();
        }else if(!q2.isEmpty()){
            return q2.peek();
        }
        return null;
    }
    public E pop(){
        if(!q1.isEmpty()){
            return q1.poll();
        }else if(!q2.isEmpty()){
            return q2.poll();
        }
        return null;
    }
    public void push(E e){
        if(q1.isEmpty() && q2.isEmpty()){
            q1.offer(e);
        }else{
            Queue qContent = q1.isEmpty()?q2:q1;
            Queue qEmpty = q2.isEmpty()?q2:q1;

            qEmpty.offer(e);
            while(!qContent.isEmpty()){
                qEmpty.offer(qContent.poll());
            }
        }
    }

    @Override
    public String toString() {
        return "MyStack{" + "q1=" + q1 + ", q2=" + q2 + '}';
    }
}

demo

public static void main(String[] args){
         MyStack stack = new MyStack();

            for (int i = 0; i < 10; i++) {
                stack.push(i);
                if(i==5){
                    stack.pop();
                }
            }
            while(!stack.isEmpty()){
                System.out.println(stack);
                System.out.println(stack.pop());
            }

            System.out.println(stack.pop());
        }

输出

MyStack{q1=[], q2=[9, 8, 7, 6, 4, 3, 2, 1, 0]}
9
MyStack{q1=[], q2=[8, 7, 6, 4, 3, 2, 1, 0]}
8
MyStack{q1=[], q2=[7, 6, 4, 3, 2, 1, 0]}
7
MyStack{q1=[], q2=[6, 4, 3, 2, 1, 0]}
6
MyStack{q1=[], q2=[4, 3, 2, 1, 0]}
4
MyStack{q1=[], q2=[3, 2, 1, 0]}
3
MyStack{q1=[], q2=[2, 1, 0]}
2
MyStack{q1=[], q2=[1, 0]}
1
MyStack{q1=[], q2=[0]}
0
null

你可能感兴趣的:(算法-面试,算法,面试,栈)