手写栈
const int N = 2e5 + 5;
struct Stack
{
int a[N];
int tot = 0;
void push(int x)
{
a[tot++] = x;
}
int pop()
{
int ret = a[tot - 1];
a[--tot] = 0;
return ret;
}
int size()
{
return tot;
}
}S;
手写队列
const int N = 2e5 + 5;
struct Queue
{
int a[N];
int l = 0, r = 0;
void push(int x)
{
a[r++] = x;
}
int pop()
{
return a[l++];
}
int size()
{
return r - l;
}
} Q;
本文介绍了如何手写实现栈和队列的数据结构。栈使用数组实现,提供了push、pop和size方法;队列同样基于数组,提供push、pop和size方法。这些基本操作对于理解和实现常见算法至关重要。
2910

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



