java Learning - Data Structure - two queues implemented stacks, two stacks queue

Two queues implemented stacks, two stacks queue

1, two stacks queue implementation

Please implemented in two queues: stack push (), pop ().

Step Reference:

(1) conventional two queues q1 and q2, q1 stack will be applied to the element
(2) when the first interpretation whether the stack is empty q1, q1 because the elements are always come after, LIFO, except the last element of the queue, to add other elements to q2, the last element q1 dequeue
(3) out of the stack when if it is determined q1 2 is empty, except for the last element q2, add q2 other elements to q1, q2 then the last element of the team

/**
 * 两个队列实现栈
 * 
 * @author Linlin Zhao
 * 
 */
public class StackAndQueue02 {
	public Queue<Integer> queue1;
	public Queue<Integer> queue2;

	public StackAndQueue02() {
		queue1 = new LinkedList<Integer>();
		queue2 = new LinkedList<Integer>();
	}

	/**
	 * 压栈(入队queue1)
	 * 
	 * @param num
	 * @return
	 */
	public void push(int num) {
			queue1.offer(num);
	}
	
	/**
	 * 弹栈
	 * @return
	 */
	public Integer pop() {
		 if (queue1.isEmpty() && queue2.isEmpty()) {
	            return null;
	        }
	        // 先判断 q1 是否为空 
	        if (!queue1.isEmpty()) {
	            int size = queue1.size();
	            for (int i = 0; i < size - 1; i++) {//其他元素转移
	                queue2.offer(queue1.poll());
	            }
	            return queue1.poll();//最后一个元素出队
	        } else {
	            int size = queue2.size();
	            for (int i = 0; i < size - 1; i++) {
	                queue1.offer(queue2.poll());
	            }
	            return queue2.poll();
	        }

	}

	public static void main(String[] args) {
		StackAndQueue02 myStack=new StackAndQueue02();
		myStack.push(3);
		myStack.push(6);
		myStack.push(8);
		myStack.push(3);
		myStack.push(2);
		myStack.push(0);
		myStack.push(1);
		
		System.out. println(myStack.pop());
		myStack.pop();
		System.out. println(myStack.pop());
			
	}
}

2, two stacks queue

Use two stacks implemented: add queue (), poll (), peek ().

Step Reference:

(1) There are two stacks stack1 stack2 and
(2) when the queue to only stack1 additive elements on the line
(3) when the first queue is empty is determined stack2, stack2 of the elements are advanced to, FIFO . If stack2 is not empty, then direct the pop-top element stack2. If you add to stack2 is empty, then stack1 elements, then pop the top element stack2

/**
 * 两个栈实现队列
 * 
 */
public class StackAndQueue {
	public Stack<Integer> stack1;
	public Stack<Integer> stack2;

	public StackAndQueue() {
		stack1 = new Stack<Integer>();
		stack2 = new Stack<Integer>();
	}

	// 入队列
	public boolean add(int num) {
		stack1.push(num);
		return true;
	}

	// 获取队首元素
	public int peek() {
		while (!stack2.isEmpty()) {
			return stack2.pop();
		}
		while (!stack1.isEmpty()) {
			stack2.push(stack1.pop());
		}
		return stack2.peek();
	}

	// 获取队首元素,并移除
	public int poll() {
		while (!stack2.isEmpty()) {
			return stack2.pop();
		}
		while (!stack1.isEmpty()) {
			stack2.push(stack1.pop());
		}
		return stack2.pop();

	}

	public static void main(String[] args) {
		StackAndQueue myQueue =new StackAndQueue();
		myQueue.add(9);
		myQueue.add(5);
		myQueue.add(8);
		myQueue.add(1);
		myQueue.add(3);
		myQueue.add(2);
		
		System.out.println(myQueue.peek());
		System.out.println(myQueue.poll());
		System.out.println(myQueue.poll());
	
	}

}

3, Dueue analog implementation Stack

/**
 * 使用队列实现自定义栈
 * 1、弹栈
 * 2、压栈
 * 3、获取头
 * @author Linlin Zhao
 *
 */
public class MyStack01<E> {

	//容器
	private Deque<E> container =new ArrayDeque<E>();
	//容量
	private int cap;
	
	public MyStack01(int cap) {
		super();
		this.cap = cap;
	}
	
	//压栈
	public boolean push(E e){
		if(container.size()+1>cap){
			return false;
		}
		return container.offerLast(e);//加到队尾
	}
	
	//弹栈
	public E pop(){
		return container.pollLast();//移除队尾
	}
	
	//获取
	public E peek(){
		return container.peekLast();//获取队尾
	}

	public int size(){
		return this.container.size();//队列长度
	}

	public static void main(String[] args) {
		
	}
}
Published 57 original articles · won praise 13 · views 1089

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105316558