news 2026/7/7 18:27:48

Java—栈与队列

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java—栈与队列

本篇来讲解栈与队列~


模块一:栈(Stack)

1. 基础知识

栈是一种后进先出(LIFO)的数据结构,只允许在一端(称为栈顶)进行插入和删除操作。核心操作包括:

  • 压栈(Push):向栈顶添加元素。
  • 弹栈(Pop):从栈顶移除元素。
  • 查看栈顶(Peek):获取栈顶元素但不移除。
  • 判空(isEmpty):检查栈是否为空。
  • 容量(Size):获取栈中元素数量。

2. 数组实现栈

使用数组实现栈时,需维护一个指向栈顶的指针(通常用top表示)。数组大小固定,需处理栈满的情况。

代码实现

public class ArrayStack { private int maxSize; // 栈的最大容量 private int[] stack; // 存储元素的数组 private int top; // 栈顶指针(初始为-1) public ArrayStack(int size) { maxSize = size; stack = new int[maxSize]; top = -1; } // 压栈 public void push(int value) { if (isFull()) { throw new RuntimeException("栈已满!"); } stack[++top] = value; } // 弹栈 public int pop() { if (isEmpty()) { throw new RuntimeException("栈为空!"); } return stack[top--]; } // 查看栈顶 public int peek() { if (isEmpty()) { throw new RuntimeException("栈为空!"); } return stack[top]; } // 判空 public boolean isEmpty() { return top == -1; } // 判满 public boolean isFull() { return top == maxSize - 1; } // 获取栈中元素数量 public int size() { return top + 1; } }

3. 双链表实现栈

双链表(双向链表)每个节点包含前驱和后继指针,实现栈时可灵活地在头部插入和删除节点(时间复杂度为O(1))。

代码实现

public class LinkedListStack { private static class Node { int value; Node prev; Node next; Node(int value) { this.value = value; } } private Node top; // 栈顶节点 private int size; // 栈中元素数量 public LinkedListStack() { top = null; size = 0; } // 压栈(在链表头部插入) public void push(int value) { Node newNode = new Node(value); if (top != null) { newNode.next = top; top.prev = newNode; } top = newNode; size++; } // 弹栈(移除链表头部节点) public int pop() { if (isEmpty()) { throw new RuntimeException("栈为空!"); } int value = top.value; top = top.next; if (top != null) { top.prev = null; } size--; return value; } // 查看栈顶 public int peek() { if (isEmpty()) { throw new RuntimeException("栈为空!"); } return top.value; } // 判空 public boolean isEmpty() { return size == 0; } // 获取栈中元素数量 public int size() { return size; } }

模块二:队列(Queue)

1. 基础知识

队列是一种先进先出(FIFO)的数据结构,元素从一端(队尾)插入,从另一端(队首)删除。核心操作包括:

  • 入队(Enqueue):向队尾添加元素。
  • 出队(Dequeue):从队首移除元素。
  • 查看队首(Peek):获取队首元素但不移除。
  • 判空(isEmpty):检查队列是否为空。
  • 容量(Size):获取队列中元素数量。

队列的典型应用场景包括任务调度(如线程池)、消息队列、广度优先搜索(BFS)等。


2. 数组实现队列(循环队列)

数组实现队列时,需解决假溢出问题(即数组前部有空间但尾部已满)。通过循环数组headtail指针循环移动)可高效利用空间。

代码实现

public class CircularQueue { private int maxSize; // 队列最大容量 private int[] queue; // 存储元素的数组 private int head; // 队首指针(指向待出队元素) private int tail; // 队尾指针(指向待插入位置) public CircularQueue(int size) { maxSize = size + 1; // 预留一个空位以区分队满和队空 queue = new int[maxSize]; head = 0; tail = 0; } // 入队 public void enqueue(int value) { if (isFull()) { throw new RuntimeException("队列已满!"); } queue[tail] = value; tail = (tail + 1) % maxSize; // 循环移动 } // 出队 public int dequeue() { if (isEmpty()) { throw new RuntimeException("队列为空!"); } int value = queue[head]; head = (head + 1) % maxSize; // 循环移动 return value; } // 查看队首 public int peek() { if (isEmpty()) { throw new RuntimeException("队列为空!"); } return queue[head]; } // 判空:head == tail public boolean isEmpty() { return head == tail; } // 判满:(tail + 1) % maxSize == head public boolean isFull() { return (tail + 1) % maxSize == head; } // 获取队列中元素数量 public int size() { return (tail - head + maxSize) % maxSize; } }

3. 双链表实现队列

双链表实现队列时,在链表尾部插入节点(入队),在头部删除节点(出队),时间复杂度均为O(1)。

代码实现

public class LinkedListQueue { private static class Node { int value; Node prev; Node next; Node(int value) { this.value = value; } } private Node head; // 队首节点 private Node tail; // 队尾节点 private int size; // 队列中元素数量 public LinkedListQueue() { head = null; tail = null; size = 0; } // 入队(在链表尾部插入) public void enqueue(int value) { Node newNode = new Node(value); if (tail == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } size++; } // 出队(移除链表头部节点) public int dequeue() { if (isEmpty()) { throw new RuntimeException("队列为空!"); } int value = head.value; head = head.next; if (head != null) { head.prev = null; } else { tail = null; // 队列已空 } size--; return value; } // 查看队首 public int peek() { if (isEmpty()) { throw new RuntimeException("队列为空!"); } return head.value; } // 判空 public boolean isEmpty() { return size == 0; } // 获取队列中元素数量 public int size() { return size; } }

总结对比

实现方式栈(时间复杂度)队列(时间复杂度)特点
数组所有操作O(1)所有操作O(1)需处理容量限制,内存连续
双链表所有操作O(1)所有操作O(1)动态扩容,内存非连续
  • 栈:优先使用java.util.Stackjava.util.Deque(如ArrayDeque)。
  • 队列:优先使用java.util.Queue的实现类(如LinkedListArrayDeque)。
  • 不过都可以使用LinkedList。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/7 13:15:22

Windows平台终极APK安装指南:告别模拟器的简单解决方案

Windows平台终极APK安装指南:告别模拟器的简单解决方案 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 还在为Windows系统无法直接安装Android应用而烦恼吗…

作者头像 李华
网站建设 2026/7/7 6:31:23

3步构建自适应量子计算Agent调度系统,你也能做到!

第一章:量子计算Agent任务调度概述在量子计算与多智能体系统融合的前沿领域,量子计算Agent任务调度正成为提升复杂计算任务执行效率的关键技术。该机制通过将传统任务调度策略与量子并行性、叠加态和纠缠特性相结合,实现对大规模问题空间的高…

作者头像 李华
网站建设 2026/7/7 15:47:26

Waydroid完整指南:在Linux系统上运行Android应用的5个实用技巧

Waydroid完整指南:在Linux系统上运行Android应用的5个实用技巧 【免费下载链接】waydroid Waydroid uses a container-based approach to boot a full Android system on a regular GNU/Linux system like Ubuntu. 项目地址: https://gitcode.com/gh_mirrors/wa/w…

作者头像 李华
网站建设 2026/7/7 18:43:31

【元宇宙数字人动作设计全攻略】:掌握5大核心技能,打造逼真Agent行为

第一章:元宇宙数字人Agent动作设计概述 在元宇宙环境中,数字人Agent作为用户化身或智能角色的核心载体,其动作设计直接影响交互的真实感与沉浸体验。动作不仅是视觉表现的基础,更是情感传递、意图表达和社交互动的重要媒介。一个具…

作者头像 李华
网站建设 2026/7/7 14:56:50

从零到百万:构建企业级SQLCoder-7B-2智能问答系统的完整指南

从零到百万:构建企业级SQLCoder-7B-2智能问答系统的完整指南 【免费下载链接】sqlcoder-7b-2 项目地址: https://ai.gitcode.com/hf_mirrors/defog/sqlcoder-7b-2 开篇:当自然语言遇到数据库查询的挑战 你是否曾经遇到过这样的困扰:…

作者头像 李华