当前位置:网站首页>---栈&队列---
---栈&队列---
2022-07-28 05:27:00 【小 乔】
目录
栈(Stack)
一、概念
栈中的元素遵循“后进先出”,允许进行插入删除的那一端称为栈顶;固定的,不允许进行插入和删除的另一端称为栈底,可以把它想象成枪上的弹夹方便理解。同时Stack是一个类,可以拿来直接用。
二、栈的核心操作:
①入栈:把元素放到栈里面,push()
②出栈:把最后进来的元素删掉,pop()
③取栈顶元素:获取最后一进来的元素的结果,peek()
三、栈的实现:
1.使用顺序表实现栈
private int[] data=new int[100];
private int size=0;a)尾插 → 入栈
public void push(int val){
if (size>=data.length){
return;
}
data[size]=val;
size++;
}b)尾删 → 出栈
public Integer pop(){
if (size==0){
return null;
}
//栈顶元素就是最后一个元素
int ret=data[size-1];
size--;
return ret;
}c)根据下标取元素 → 取栈顶元素
public Integer peek(){
if (size==0){
return null;
}
return data[size-1];
}2.使用链表实现栈
定义节点
class Node{
int val;
Node next;
public Node(int val) {
this.val = val;
}
}(此处使用不带傀儡结点的链表来表示)
a)头插 → 入栈
public void push(int val){
Node newNode=new Node(val);
//把新结点进行头插
//由于当前链表不带傀儡结点,所以先判断是否为空
if (head==null){
head=newNode;
return;
}
newNode.next=head;
head=newNode;
}b)头删 → 出栈
public Integer pop(){
//进行头删
if (head==null){
return null;
}
if (head.next==null){
int ret= head.val;
head=null;
return ret;
}
int ret=head.val;
head =head.next;
return ret;
}c)取头节点 → 取栈顶元素
public Integer peek(){
if (head==null){
return null;
}
return head.val;
}队列(Queue)
一、概念
队列中的元素遵循“先进先出”,允许删除的一端称为队首,允许插入的一端称为队尾
和栈不同,队列是一个接口,不能直接实例化,需要创建对应的子类。
二、复杂队列举例:
1.优先队列
2.消息队列(队列中的元素带有类型,出队列的时候可以按照类型来取元素)
3.阻塞队列(如果队列满,此时继续插入数据,就会形成“阻塞”,如果队列为空,此时继续取元素也会形成“阻塞”【阻塞:通俗来讲就是代码不会继续往下走】)
4.无锁队列(更高效的线程安全的队列,保证线程安全的常见手段是加锁,但是加锁效率比较低)
三、队列的实现
1.使用链表实现队列
定义结点
static class Node{
int val;
Node next;
public Node(int val){
this.val=val;
}
}创建链表的头结点和尾结点
private Node head=null;
private Node tail=null;a)入队列 offer(int val)
public boolean offer(int val){
Node newNode=new Node(val);
//插入到链表尾部,需要考虑当前列表是否为空
if (head==null){
//直接让head和tail指向新结点即可
head=newNode;
tail=newNode;
return true;
}
tail.next=newNode;
tail=tail.next;
return true;
}b)出队列 poll()
public Integer poll() {
if (head==null){
return null;
}
int ret=head.val;
if (head.next==null){
head=null;
return ret;
}
head=head.next;
return ret;
}c)取队首元素 peek(int val)
public Integer peek(){
if (size==0){
return null;
}
return data[size-1];
}2.使用数组实现环形队列
创建俩个下标,分别表示队列的头部和尾部,专门设一个size变量记录,size==0为空,size==数组长度为满。
private int[] data=new int[100];
//队列有效区间[head,tail)
private int head=0;
private int tail=0;
private int size=0;a)入队列
public boolean offer(int val){
if (size==data.length){
//队列满了,此处也可以实现扩容逻辑
return false;
}
//把新元素放到tail对应的下标上
data[tail]=val;
tail++;
//tail到达了数组末尾,就需要让tail从头开始
if (tail==data.length){
tail=0;
}
size++;
return true;
}b)出队列
public Integer poll(){
if (size==0){
return null;
}
int ret=data[head];
head++;
if (head==data.length){
head=0;
}
size--;
return ret;
}c)取队首元素
public Integer peek(){
if (size==0){
return null;
}
return data[head];
}边栏推荐
- OJ 1018 counting game
- Question skimming record - hash table
- AQS之semaphore源码分析
- Everything you don't know about time complexity is here
- Mongodb replica set and partitioned cluster
- [dynamic planning -- the best period for buying and selling stocks Series 2]
- Skimming records -- sequence traversal of binary tree
- 准备开始写博客了
- [dynamic planning -- the best period for buying and selling stocks series 3]
- OJ 1131 beautiful number
猜你喜欢

What is hash? (development of Quantitative Trading Robot System)

redis缓存设计与性能优化

Graphic pipeline foundation (part outside)

STM32的IAP跳转相关bug经历

Leetcode brush question diary sword finger offer II 055. binary search tree iterator

Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree

Bug experience related to IAP jump of stm32
![[queue, simple application of stack ---- packaging machine]](/img/bc/617b1eb35558c4f948018f593a1de5.jpg)
[queue, simple application of stack ---- packaging machine]
![[pta ---- traversal of tree]](/img/d8/260317b30d624f8e518f8758706ab9.png)
[pta ---- traversal of tree]

mongoDB快速入门
随机推荐
[dynamic planning -- the best period series for buying and selling stocks]
NIO示例
MySQL index optimization
Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
Everything you don't know about time complexity is here
[dynamic planning -- the best period for buying and selling stocks series 3]
Project compilation nosuch*** error problem
RayMarching实现体积光渲染
OJ 1131 美丽数
软件测试(概念篇)
图形管线基础(二)
图形管线基础(番外篇)
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器
动态规划--简单题型之爬楼梯
Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
SSAO By Computer Shader(一)
yapi漏洞挂马程序chongfu.sh处理
OJ 1253 ordering problem
OJ 1020 最小的回文数
Analysis of the semaphore source code of AQS