Implement Stack using Queues

原创 2015年07月07日 15:45:11

1 题目描述

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
题目出处:https://leetcode.com/problems/implement-stack-using-queues/


2 解题思路

1.题目的意思是使用队列实现栈的操作,这两个数据结构的主要区别在于一个将元素加入到尾部,一个将元素加入头部,因此考虑使用两个队列来处理这两种差异方式,始终保持一个队列为空,每次添加元素时,添加到空队列中,然后将非空队列添加到该队列中,并清空。

2.注意一下java中Queue的用法,参考网址:http://www.cnblogs.com/linjiqin/archive/2013/05/30/3107656.html


3 源代码

package com.larry.easy;

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

/**
 * 使用两个Queue来实现栈
 * @author admin
 *
 */
public class ImplementStackUsingQueues {
	//始终保持一个队列为空;添加时,添加到空队列,然后将另一个加入
	private Queue<Integer> que1 = new LinkedList<Integer>();//使用Queue时采用这种方式
	private Queue<Integer> que2 = new LinkedList<Integer>();
	
	// Push element x onto stack.
    public void push(int x) {
        if(que1.isEmpty()) {
        	que1.offer(x);
        	que1.addAll(que2);
        	que2.clear();
        }else{
        	que2.offer(x);
        	que2.addAll(que1);
        	que1.clear();
        }
    }

    // Removes the element on top of the stack.
    public void pop() {
        if(!que1.isEmpty()) que1.poll();
        if(!que2.isEmpty()) que2.poll();
    }

    // Get the top element.
    public int top() {
    	if(!que1.isEmpty()) return que1.peek();
    	if(!que2.isEmpty()) return que2.peek();
        return 0;
    }

    // Return whether the stack is empty.
    public boolean empty() {
    	if(que1.isEmpty() && que2.isEmpty()) return true;
    	else return false;
    }
    
    public static void main(String[] args) {
		ImplementStackUsingQueues isuq = new ImplementStackUsingQueues();
		isuq.push(1);
		isuq.push(2);
		isuq.pop();
		System.out.println(isuq.top());
	}
}


相关文章推荐

【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】

【225-Implement Stack using Queues(用队列实现栈操作)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.co...

leetcode 225. Implement Stack using Queues

题目Implement the following operations of a stack using queues.push(x) – Push element x onto stack. p...

关于LeetCode中Implement Stack using Queues一题的理解

题目如下: Implement the following operations of a stack using queues. push(x) -- Push element x on...

[leetcode: Python]225. Implement Stack using Queues

题目: mplement the following operations of a stack using queues.push(x) – Push element x onto stack. ...
  • NXHYD
  • NXHYD
  • 2017年05月12日 12:54
  • 185

Leetcode225. Implement Stack using Queues

该题和232题类似,思想上差不多232题 原题 Implement the following operations of a stack using queues.push(x) – Push ...

LeetCode(36)- Implement Stack using Queues

题目:Implement the following operations of a stack using queues.push(x) -- Push element x onto stack. ...

【LeetCode】Implement Stack using Queues 解题报告

【题目】 Implement the following operations of a stack using queues. push(x) -- Push element x o...
  • ljiabin
  • ljiabin
  • 2015年06月14日 10:35
  • 8501

[LeetCode]225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack....

Leetcode 225 Implement Stack using Queues 使用队列实现栈

使用队列来实现一个栈

Leetcode_252_Implement Stack using Queues

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598773 Implement the fo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Implement Stack using Queues
举报原因:
原因补充:

(最多只允许输入30个字)