java学习——数据结构——两个队列实现栈、两个栈实现队列

两个队列实现栈、两个栈实现队列

1、两个队列实现栈

请用两个队列实现:栈的push()、pop()。

步骤参考:

(1)现有两个队列 q1 和 q2,入栈则将元素加到 q1
(2)出栈的时候先判读 q1 是否为空,因为 q1 中的元素总是后进来的,后进先出,除了队列的最后一个元素,将其它元素添加到 q2,q1 的最后一个元素出队
(3)出栈的时候如果在 2 中判断 q1 为空,除了 q2 的最后一个元素,将 q2 中其它元素添加到 q1,然后 q2 中的最后一个元素出队

/**
 * 两个队列实现栈
 * 
 * @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、两个栈实现队列

请用两个栈实现:队列的add()、poll()、peek()。

步骤参考:

(1)有两个栈 stack1 和 stack2
(2)入队列的时候只往 stack1 添加元素就行
(3)出队列的时候先判断 stack2 是否为空,stack2 中的元素都是先进来的,先进先出。如果 stack2 不为空,则直接弹出 stack2 的栈顶元素。如果为空,则将 stack1 的元素添加到 stack2 中,然后弹出 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模拟实现栈

/**
 * 使用队列实现自定义栈
 * 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) {
		
	}
}
发布了57 篇原创文章 · 获赞 13 · 访问量 1089

猜你喜欢

转载自blog.csdn.net/weixin_42924812/article/details/105316558
今日推荐