堆栈

简介: 堆栈

堆栈(英语:stack)又称为堆叠,是计算机科学中的一种抽象数据类型,只允许在有序的线性数据集合的一端(称为堆栈顶端,英语:top)进行加入数据(英语:push)和移除数据(英语:pop)的运算。因而按照后进先出(LIFO, Last In First Out)的原理运作。

640.png

LeetCode232题


两个栈组成一个队列



思想:两个栈,一个input ,一个 output , 进入队列都从 input 输入,出队列都从output 出。但是要注意的是 pop, 或者 peek 时,注意如果output 是空的需要将input 中的内容都放到 output 中。如果 output 不为空,就将 input 倒入,会破坏队列的结构。


import java.util.Stack;
/**
 * 类说明 使用栈实现队列的下列操作:
 * 
 * push(x) -- 将一个元素放入队列的尾部。pop() -- 从队列首部移除元素。peek() -- 返回队列首部的元素。empty() --
 * 返回队列是否为空。
 * 
 * 示例:
 * 
 * MyQueue queue = new MyQueue();
 * 
 * queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1
 * queue.empty(); // 返回 false
 * 
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/implement-queue-using-stacks
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * wangm          2020年4月18日    Create this file
 * </pre>
 * 
 */
class MyQueue {
    Stack<Integer> input;
    Stack<Integer> output;
    /** Initialize your data structure here. */
    public MyQueue() {
        input = new Stack<Integer>();
        output = new Stack<Integer>();
    }
    public void transfer() {
        if (output.isEmpty()) {
            while (!input.isEmpty()) {
                int x = input.pop();
                output.push(x);
            }
        }
    }
    /** Push element x to the back of queue. */
    public void push(int x) {
        input.push(x);
    }
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        transfer();
        return output.pop();
    }
    /** Get the front element. */
    public int peek() {
        transfer();
        return output.peek();
    }
    /** Returns whether the queue is empty. */
    public boolean empty() {
        if (input.isEmpty() && output.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
/**
 * 
 * 类说明 思想: 两个栈,一个input ,一个 output , 进入队列都从 input 输入,出队列都从output 出。但是要注意的是 pop,
 * 或者 peek 时,注意如果output 是空的需要将input 中的内容都放到 output 中。 如果 output 不为空,就将 input
 * 倒入,会破坏队列的结构。
 * 
 * <pre>
 * Modify Information:
 * Author        Date          Description
 * ============ =========== ============================
 * wangm          2020年4月18日    Create this file
 * </pre>
 *
 */
public class LeetCode232 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek()); // 返回 1
        System.out.println(queue.pop()); // 返回 1
        System.out.println(queue.empty()); // 返回 false
    }
}


相关文章
|
15天前
|
存储 算法 C++
四则计算机实现(C++)(堆栈的应用)
四则计算机实现(C++)(堆栈的应用)
|
6月前
|
存储 算法 C语言
5.堆栈算法
5.堆栈算法
|
9月前
|
存储 Java
堆栈的区别是什么
堆和栈是计算机内存中两种不同的数据结构,它们用来存储程序运行时所需的数据。虽然堆和栈都是用于存储数据的,但它们在内存管理和数据访问方面有着明显的区别。下面我将详细解释堆和栈的区别。
174 0
特殊堆栈
数据结构栈的使用
|
Java 中间件 Unix
JVM:如何分析线程堆栈
英文原文:JVM: How to analyze Thread Dump 在这篇文章里我将教会你如何分析JVM的线程堆栈以及如何从堆栈信息中找出问题的根因。在我看来线程堆栈分析技术是Java EE产品支持工程师所必须掌握的一门技术。
1642 0
freertos任务堆栈的描述
freertos任务堆栈的描述
103 0
freertos任务堆栈的描述
|
C++ API 数据建模
Windbg查看调用堆栈(k*)
https://www.52pojie.cn/thread-664189-1-1.html       无论是分析程序崩溃原因,还是解决程序hang问题,我们最常查看的就是程序调用堆栈。
1776 0
|
NoSQL C++ C语言
C/C++中手动获取调用堆栈【转】
转自:http://blog.csdn.net/kevinlynx/article/details/39269507 版权声明:本文为博主原创文章,未经博主允许不得转载。 当我们的程序core掉之后,如果能获取到core时的函数调用堆栈将非常有利于定位问题。
1076 0

热门文章

最新文章