当前位置:网站首页>---栈&队列---
---栈&队列---
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];
}边栏推荐
猜你喜欢

SSAO by computer shader (II)

mongoDB快速入门

Leetcode skimming diary sword finger offer II 050. sum of downward path nodes

如何描述一个BUG以及BUG级别的定义、生命周期
![[哈希表基础知识]](/img/8f/54a4780a02f81e5de3d92c25248e1e.png)
[哈希表基础知识]
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]
![[dynamic planning -- the best period for buying and selling stocks series 3]](/img/9f/f6c07264f5ffaa0fdfcc724c713e83.png)
[dynamic planning -- the best period for buying and selling stocks series 3]

mongoDB复制集及分片集群
![[C note] data type and storage](/img/3d/6b7a848dff5a8c0ccd0a54c19bce46.png)
[C note] data type and storage

NIO示例
随机推荐
[queue, simple application of stack ---- packaging machine]
Leetcode 刷题日记 剑指 Offer II 047. 二叉树剪枝
feignclient @RequestMapping参数设置及请求头简易方式设置
OJ 1253 点菜问题
Valgrind tool
Code tidiness (I)
Code neatness (2)
OJ 1451 数字游戏
战疫杯--我的账本
OJ 1018 counting game
Pyppeteer is recognized to bypass detection
OJ 1089 春运
OJ 1505 fuse
@PostConstruct注解及用处示例
[哈希表基础知识]
explain详解
下雨场景效果(一)
Battle plague Cup -- strange shape
[pta---- output full arrangement]
2021-11-10