当前位置: 首页 > news >正文

数据结构(java)栈与队列

栈:(先进后出)

        入栈:
1.普通栈一定要放、最小栈放的原则是:

    *如果最小栈是空的,那么
    *如果最小栈的栈顶元素没有当前的元素小,则
2.如果要放的的元素小于等于最小栈栈顶元素可以放吗?
       出栈:
   需要判断 出栈的元素 和 栈顶元素是否相同,相同则最小栈也要出栈

队列:(先进先出)

      单链表实现队列:

public class MyQueue {static class ListNode {public int val;public ListNode prev;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode first = null;public ListNode last = null;public int usedSize = 0;public void offer(int val) {ListNode node = new ListNode(val);if(isEmpty()) {first = last = node;}else {last.next = node;node.prev = last;last = last.next;}usedSize++;}public int poll() {if(isEmpty()) {return -1;}int val = first.val;first = first.next;if(first != null) {first.prev = null;}usedSize--;return val;}public int peek() {if(isEmpty()) {return -1;}return first.val;}public boolean isEmpty() {return usedSize == 0;}}

设置循环队列:

class MyCircularQueue {public int front;public int rear;public int[] elem;public MyCircularQueue(int k) {elem = new int[k+1];}//入队列 public boolean enQueue(int value) {if(isFull()) {return false;}elem[rear] = value;rear = (rear+1)%elem.length;return true;}//出队列 public boolean deQueue() {if(isEmpty()) {return false;}front = (front+1)%elem.length;return true;}//得到队头元素 public int Front() {if(isEmpty()) {return -1;}return elem[front];}public int Rear() {if(isEmpty()) {return -1;}int index = (rear == 0) ? elem.length-1 : rear-1;return elem[index];}public boolean isEmpty() {return rear == front;}public boolean isFull() {return (rear+1)%elem.length == front;}
}

用队列实现栈:

import java.util.LinkedList;
import java.util.Queue;class MyStack {private Queue<Integer> queue;public MyStack() {queue = new LinkedList<>();}public void push(int x) {// 每次push时,将新元素加入队列,然后将前面的元素依次出队再入队// 这样新元素就在队列前端,模拟了栈的后进先出特性queue.offer(x);int size = queue.size();for (int i = 0; i < size - 1; i++) {queue.offer(queue.poll());}}public int pop() {if (empty()) {throw new RuntimeException("Stack is empty");}return queue.poll();}public int top() {if (empty()) {throw new RuntimeException("Stack is empty");}return queue.peek();}public boolean empty() {return queue.isEmpty();}}

用栈实现队列:、

import java.util.ArrayDeque;
class MyQueueUseStack {public ArrayDeque<Integer> stack1;public ArrayDeque<Integer> stack2;public MyQueueUseStack() {stack1 = new  ArrayDeque<>();stack2 = new  ArrayDeque<>();}public void push(int x) {stack1.push(x);}public int pop() {if(empty()) {return -1;}if(stack2.isEmpty()) {//第一个栈里面所有的元素 放到第二个栈当中while(!stack1.isEmpty()) {stack2.push(stack1.pop());}}return stack2.pop();}public int peek() {if(empty()) {return -1;}if(stack2.isEmpty()) {//第一个栈里面所有的元素 放到第二个栈当中while(!stack1.isEmpty()) {stack2.push(stack1.pop());}}return stack2.peek();}public boolean empty() {return stack1.isEmpty() && stack2.isEmpty();}}

http://www.dtcms.com/a/137467.html

相关文章:

  • Chemical Review IF=51.4 综述 | 柔性机器人的当下与未来:材料、技术与应用的深度融合
  • STM32F103C8T6-基于FreeRTOS系统实现步进电机控制
  • GD32裸机程序-SFUD接口文件记录
  • 用 C++ 模拟客户端渲染中的分步数据加载
  • CVFSNet:一种用于端到端脑梗塞溶栓治疗后改良脑梗死溶栓分级(mTICI)评分的跨视图融合评分网络|文献速递-深度学习医疗AI最新文献
  • 使用CubeMX新建SysTick延时函数工程——使用中断,不使用HAL_Delay
  • 【QT入门到晋级】QT打动态库包及引入动态库包
  • std visit
  • centos部署的openstack发布windows虚拟机
  • 卷积神经网络 CNN 模型介绍
  • 使用DeepSeek如何提升课题申报书中研究内容的专业性?25个进阶DeepSeek指令
  • QT —— 信号和槽(自定义信号和槽函数)
  • 《Timer: Generative Pre-trained Transformers Are Large Time Series Models》
  • C++Cherno 学习笔记day20 [81]-[85] 可视化基准测试、单例模式、小字符串优化sso、跟踪内存分配、左值与右值
  • 蓝桥杯B组Java省赛强化
  • Cribl (实验) vpc-flow 数据抽样
  • 中科院1区顶刊Expert Systems with Applications ESO:增强型蛇形算法,性能不错
  • LeetCode -- Flora -- edit 2025-04-16
  • Numpy常用库方法总结
  • Langchain + Gemini API调用基本操作
  • MGR实现mysql高可用性
  • JVM:垃圾回收
  • JVM:运行时数据区和线程
  • 【计算机方向】中科院双一区TOP顶刊,IF=14.7,Nature招牌1区Top,中一篇直接就稳了!
  • map用法介绍
  • 前沿计组知识入门(六)
  • Arkts应用全局UI状态存储和持久化V2(AppStorageV2、PersistenceV2和@Type)
  • Idea集成AI:CodeGeeX开发
  • 【Leetcode-Hot100】缺失的第一个正数
  • 【LangChain核心组件】Memory:让大语言模型拥有持续对话记忆的工程实践