代码随想录 Day10 - 栈与队列(上)

简介: 代码随想录 Day10 - 栈与队列(上)

相关概念



先进后出,主要 API:push(), pop(), Java 中的主要实现类 Stack()


队列


先进先出,主要 API:offer(), poll(), peek(),Java 中的主要实现类 Queue 接口,如 LinkedList


作业题


232. 用栈实现队列

package jjn.carl.stack_queue;
import java.util.Stack;
/**
 * @author Jjn
 * @since 2023/7/7 22:50
 */
public class MyQueue {
    private final Stack<Integer> first;
    private final Stack<Integer> second;
    public MyQueue() {
        this.first = new Stack<>();
        this.second = new Stack<>();
    }
    public void push(int x) {
        first.push(x);
    }
    public int pop() {
        if (second.isEmpty()) {
            while (!first.isEmpty()) {
                second.push(first.pop());
            }
        }
        return second.pop();
    }
    public int peek() {
        if (second.isEmpty()) {
            while (!first.isEmpty()) {
                second.push(first.pop());
            }
        }
        return second.peek();
    }
    public boolean empty() {
        return first.isEmpty() && second.isEmpty();
    }
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println("myQueue.peek() = " + myQueue.peek());
        System.out.println("myQueue.pop() = " + myQueue.pop());
        System.out.println("myQueue.empty() = " + myQueue.empty());
    }
}


225. 用队列实现栈

package jjn.carl.stack_queue;
import java.util.LinkedList;
import java.util.Queue;
/**
 * @author Jjn
 * @since 2023/7/7 23:03
 */
public class MyStack {
    private final Queue<Integer> first;
    private final Queue<Integer> second;
    public MyStack() {
        this.first = new LinkedList<>();
        this.second = new LinkedList<>();
    }
    public void push(int x) {
        while (!first.isEmpty()) {
            second.offer(first.poll());
        }
        first.offer(x);
        while (!second.isEmpty()) {
            first.offer(second.poll());
        }
    }
    public int pop() {
        return first.peek() == null ? -1 : first.poll();
    }
    public int top() {
        return first.peek() == null ? -1 : first.peek();
    }
    public boolean empty() {
        return first.isEmpty();
    }
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println("myStack.top() = " + myStack.top());
        System.out.println("myStack.pop() = " + myStack.pop());
        System.out.println("myStack.empty() = " + myStack.empty());
    }
}



目录
相关文章
|
2天前
|
存储 算法 搜索推荐
探索常见数据结构:数组、链表、栈、队列、树和图
探索常见数据结构:数组、链表、栈、队列、树和图
76 64
|
10天前
|
算法 安全 测试技术
golang 栈数据结构的实现和应用
本文详细介绍了“栈”这一数据结构的特点,并用Golang实现栈。栈是一种FILO(First In Last Out,即先进后出或后进先出)的数据结构。文章展示了如何用slice和链表来实现栈,并通过golang benchmark测试了二者的性能差异。此外,还提供了几个使用栈结构解决的实际算法问题示例,如有效的括号匹配等。
golang 栈数据结构的实现和应用
|
2天前
|
缓存 算法 调度
数据结构之 - 双端队列数据结构详解: 从基础到实现
数据结构之 - 双端队列数据结构详解: 从基础到实现
16 5
|
2天前
|
Go
数据结构之 - 深入了解栈数据结构
数据结构之 - 深入了解栈数据结构
11 5
|
2天前
|
消息中间件 存储 Java
数据结构之 - 深入探析队列数据结构: 助你理解其原理与应用
数据结构之 - 深入探析队列数据结构: 助你理解其原理与应用
11 4
|
2天前
【初阶数据结构】深入解析队列:探索底层逻辑
【初阶数据结构】深入解析队列:探索底层逻辑
|
1天前
|
算法 开发者 计算机视觉
燃爆全场!Python并查集:数据结构界的网红,让你的代码炫酷无比!
在编程的世界里,总有一些数据结构以其独特的魅力和高效的性能脱颖而出,成为众多开发者追捧的“网红”。今天,我们要介绍的这位明星,就是Python中的并查集(Union-Find)——它不仅在解决特定问题上大放异彩,更以其优雅的设计和强大的功能,让你的代码炫酷无比,燃爆全场!
7 0
|
2天前
|
算法 Java 测试技术
数据结构 —— Java自定义代码实现顺序表,包含测试用例以及ArrayList的使用以及相关算法题
文章详细介绍了如何用Java自定义实现一个顺序表类,包括插入、删除、获取数据元素、求数据个数等功能,并对顺序表进行了测试,最后还提及了Java中自带的顺序表实现类ArrayList。
5 0
|
2天前
|
存储
【初阶数据结构】深入解析栈:探索底层逻辑
【初阶数据结构】深入解析栈:探索底层逻辑
|
11天前
01_设计一个有getMin功能的栈
01_设计一个有getMin功能的栈