1.c++手写栈
#include <iostream>
using namespace std;
using i64 = long long;
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define bug(x) cerr << x << "\n"
#define Bug(a) for (int i = 0; i < sz(a); i++) { cerr << a[i] << " "; } cerr << "\n"
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<i64, i64> pll;
const int N = 1e5 + 10;
const int MOD = 998244353;
const int INF = (int) 1e9;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
template<class T>
class stack {
public:
T stk[N];
int top;
stack() {
top = 0;
}
bool isFull();
bool isEmpty();
void push(T x);
T pop();
};
template<class T>
bool stack<T>::isFull() {
if (top == N) {
return 1;
}
return 0;
}
template<class T>
bool stack<T>::isEmpty() {
if (top == 0) {
return 1;
}
return 0;
}
template<class T>
T stack<T>::pop() {
if (isEmpty()) {
return -1;
}
return stk[--top];
}
template<class T>
void stack<T>::push(T x) {
if (isFull()) {
return;
}
stk[top++] = x;
}
int main() {
stack<double> st;
st.push(1.1);
st.push(2.2);
st.push(3.3);
while (!st.isEmpty()) {
cout << st.pop() << endl;
}
return 0;
}
2.手写队列
#include <iostream>
using namespace std;
using i64 = long long;
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define bug(x) cerr << x << "\n"
#define Bug(a) for (int i = 0; i < sz(a); i++) { cerr << a[i] << " "; } cerr << "\n"
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<i64, i64> pll;
const int N = 1e5 + 10;
const int MOD = 998244353;
const int INF = (int) 1e9;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
template<class T>
class queue {
public:
T que[N];
int head, tail;
queue() {
head = tail = 0;
}
bool isFull();
bool isEmpty();
void push(T x);
T pop();
};
template<class T>
bool queue<T>::isFull() {
if (tail == N) {
return 1;
}
return 0;
}
template<class T>
bool queue<T>::isEmpty() {
if (tail == head) {
return 1;
}
return 0;
}
template<class T>
T queue<T>::pop() {
if (isEmpty()) {
return -1;
}
return que[head++];
}
template<class T>
void queue<T>::push(T x) {
if (isFull()) {
return;
}
que[tail++] = x;
}
int main() {
queue<int> que;
que.push(1);
que.push(2);
que.push(3);
while (!que.isEmpty()) {
cout << que.pop() << endl;
}
return 0;
}