介绍
用了模板类。
实现代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std;
#define fori(i, a, b) for (int i = (a); i < (b); i++)
#define ford(i, b, a) for (int i = (b) - 1; i >= a; i--)
#define trav(a, arr) for (auto& a : arr)
#define all(x) (x).begin(), (x).end()
const int maxn = 100;
const int base_size = 25;
struct stack_node
{
void* root;
int data_type; // 1 int 2 double 3 char
}st[maxn + 1];
template<class T>class stack
{
private:
T* datas;
int max_size, pointer;
public:
stack<T>(int max_size) {
datas = (T*)malloc(sizeof(T) * max_size), this->max_size = max_size, this->pointer = 0; }
stack<T>(void) {
datas = (T*)malloc(sizeof(T) * base_size), this->max_size = base_size, this->pointer = 0; }
~stack(void) {
free(datas); }
int size(void) {
return this->pointer; }
void push(T data) {
(this->pointer == this->max_size ? this->datas = (T*)realloc(this->datas, (this->max_size + base_size) * sizeof(T)) : 0), this->datas[this->pointer++] = data; }
void pop(void) {
if (this->pointer) this->pointer--; }
bool empty(void) {
return this->pointer == 0; }
void clear(void) {
this->pointer = 0; }
T top(void) {
return this->pointer == 0 ? NULL : this->datas[pointer - 1]; }
};
int