栈操作
- push()
- pop()
- top()
- empty()
- size()
顺便温习下 模版template, 结构体struct, 链表linked list, 指针pointer.(这是来搞翻译的么…)
贴码:
- 还非得整两个文件,两个文件在一个文件夹中即可,可以g++手动编译下
g++ -o stack main.cpp
- 合并到一个文件中也行。
文件stack.h
// stack.h
#ifndef STACK_H
#define STACK_H
#include <ostream>
using namespace std;
template <class T>
class Stack{
public:
Stack();
Stack(int max);
~Stack();
void Push(T const &x);
void Pop();
const T Top() const;
size_t Size() const { return count; }
bool IsEmpty() const { return count==0; }
private:
struct Node {
T data;
Node *next;
};
typedef Node node; //类型别名
node *top;
size_t count;
int max;
};
template <class T>
Stack<T>::Stack():top(nullptr), count(0), max(-1){
cout << "init, come on!" << endl;
cout << "at the first, count = " << count << endl;
}
template <class T>
Stack<T>::Stack(int max):top(