数据结构算法-栈、队列、优先队列、双端队列(三)
原文链接: https://www.cnblogs.com/bigroc/p/14212875.html
一、Stack (栈)
1、数据结构
Stack是栈。它的特性是:先进后出(FILO, First In Last Out) 后进先出(Last in - First out)。java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组实现的,这就意味着,Stack也是通过数组实现的,而非链表。当然,我们也可以将LinkedList当作栈来使用。
2、Stack的继承关系
3、时间复杂度
操作 | 时间复杂度 |
---|---|
lookup | O(n) |
insert | O(1) |
delete | O(1) |
4、源码实现
Stack
继承自 Vector
底层为动态数组实现
/**
* 向栈顶添加元素
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item); // Vector 中的新增方法
return item;
}
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
* 查看栈顶元素并删除
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1); // Vector 中的按照下标移除元素的方法
return obj;
}
/**
* Looks at the object at the top of this stack without removing it
* from the stack.
* 查看栈顶元素但不删除
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
// 栈是否为空
public boolean empty() {
return size() == 0;
}
// 查找“元素o”在栈中的位置:由栈底向栈顶方向数
public synchronized int search(Object o) {
// 获取元素索引,elementAt()具体实现在Vector.java中
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
二、Queue (队列)
1、数据结构
First in - First out(先进先出 FIFO)
2、时间复杂度
操作 | 时间复杂度 |
---|---|
lookup | O(n) |
insert | O(1) |
delete | O(1) |
3、源码实现
//接口Queue:
public interface Queue<E> extends Collection<E> {
//将指定元素插入到队列的尾部(队列满了话,会抛出异常)
boolean add(E e);
//将指定元素插入此队列的尾部(队列满了话,会返回false)
boolean offer(E e);
/返回取队列头部的元素,并删除该元素(如果队列为空,则抛出异常)
E remove();
//返回队列头部的元素,并删除该元素(如果队列为空,则返回null)
E poll();
//返回队列头部的元素,不删除该元素(如果队列为空,则抛出异常)
E element();
//返回队列头部的元素,不删除该元素(如果队列为空,则返回null)
E peek();
}
三、Deque (Double-Ended Queue 双端队列)
1、数据结构
线性集合,支持两端插入和移除元素。名称deque是“双端队列(double ended queue)”的缩写。
也可以理解为 Queue 与 Stack 的结合体,可以从头部 Push 或者 Pop 元素,也可以在尾部 Push 或者 Pop 元素,两端可以进出的队列
2、Deque 的继承关系
3、方法摘要
First Element (Head) | Last Element (Tail) | |||
Throws exception | Special value | Throws exception | Special value | |
Insert(插入操作) | addFirst(e) |
offerFirst(e) |
addLast(e) |
offerLast(e) |
Remove(移除操作) | removeFirst() |
pollFirst() |
removeLast() |
pollLast() |
Examine(检索操作) | getFirst() |
peekFirst() |
getLast() |
peekLast() |
4、时间复杂度
操作 | 时间复杂度 |
---|---|
lookup | O(n) |
insert | O(1) |
delete | O(1) |
5、源码实现
四、Priority Queue (优先队列)🌶️
1、数据结构
底层具体实现的数据结构较为多样和复杂可以使用 heap、bst(binary search tree)、treap
2、时间复杂度
操作 | 时间复杂度 |
---|---|
插入 | O(1) |
取出 | O(logN) |
3、源码实现 & 文档
它的使用与队列几乎没有区别,只不过我们在取元素的时候需要注意都是取优先级最高的,优先级怎么定义?我们只需要将元素取实现 comparator
(比较器)就可以由大到小
或由小到大
再或者 自己定义某个字段优先等等。
Java 的 PriorityQueue 文档
Java 的 PriorityQueue 源码
本文来自博客园,作者:bigroc,转载请注明原文链接:https://www.cnblogs.com/bigroc/p/14212875.html
blog:http://www.bigroc.cn 博客园:https://www.cnblogs.com/bigroc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具