AKSZ-栈与队列
2024-05-02 17:16:17
发布于:广东
栈
定义
线性的数据结构
数组实现栈
#include<bits/stdc++.h>
using namespace std;
//定义栈数组
int stk[100005];
int top;//栈数组多少
void init(){
top=0;
}
void add(int x){
stk[++top]=x;//入栈
}
void pop(){
top--;//出栈
}
bool empty(){//表示栈是否为空
return !top;
}
int query(){//查询栈顶元素
if(top)//一定要有元素
return stk[top];
}
int main(){
}
定义
stack<int> stk; //栈名
入栈
stk.push(变量名); //入栈
出栈
stk.pop(); //出栈
查看栈顶
int tmp=stk.top();//栈顶
表示栈是否为空
stk.empty();//表示栈是否为空
栈大小
stk.size();//栈大小
队列
定义
queue<int> q; //队名
入队
q.push(变量名); //入队
删除队头
q.pop(); //删除队头
取队头元素
int tmp=q.front();//取队头元素
表示队列是否为空
q.empty();//表示队列是否为空
队列长度
q.size();//队列长度
清空队列
while(!q.empty){
q.pop();
}
优先队列
定义
priority_queue<int>q;
取最大值
q.top();
删除最大值
q.pop();
放入元素
q.push();
取最小值
priority_queue<int,vector<int>,greater<int> >q;
重载
struct node{
int x,y;
//重载
friend bool operator <(node a,node b){
return a.x < b.x;
}
};
这里空空如也
有帮助,赞一个