栈,队列
#include
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int top;
//1.push(x) :向栈中加⼊⼀个数x
void push(int x)
{
arr[++top] = x;
}
//2.出栈
void pop()
{
top--;
}
//3.查询栈顶元素
void front()
{
cout << arr[top];
}
//4.判断栈是否为空
bool empty()
{
return top == 0;
}
//5. 栈中元素个数
int size()
{
return top;
}
//stack
//1. 如何创建?
//2. 关⼼⾥⾯有什么函数?
//1 创建
//stack st;
//T 可以是任意类型的数据。
//2 size / empty
//1. size :返回栈⾥实际元素的个数;
//2. empty :返回栈是否为空。
//3 push / pop
//1. push :进栈;
//2. pop :出栈。
//4 top
//1. top :返回栈顶元素,但是不会删除栈顶元素。
//栈,先进后出
//队列(先进先出)
const int N = 1e5 + 10;
int q[N], h, t;
// 1.⼊队
void push(int x)
{
q[++t] = x;
}
// 2.出队
void pop()
{
h++;
}
// 3.查询队头元素
int front()
{
return q[h + 1];
}
// 4.查询队尾元素
int back()
{
return q[t];
}
// 5.判断是否为空
bool empty()
{
return h == t;
}
// 有效元素的个数
int size()
{
return t - h;
}
//queue
//1. 如何创建?
//2. ⾥⾯提供了什么函数?
//1 创建
//queue q;
//T 可以是任意类型的数据。
//2 size / empty
//1. size :返回队列⾥实际元素的个数;
//2. empty :返回队列是否为空。
//3 push / pop
//1. push :⼊队;
//2. pop :出队。
//4 front / back
//1. front :返回队头元素,但不会删除;
//2. back :返回队尾元素,但不会删除。
从0实现栈与队列(含STL)

被折叠的 条评论
为什么被折叠?



