Stack_Y
saxiy
·
·
个人记录
#include <iostream>
using namespace std;
template<class T>
class node_S {
public:
T data;
node_S<T> *next;
node_S() { next = NULL; }
node_S(T a) { next = NULL; data = a; }
};
template<class T>
class SY{
private:
int nodesum;
node_S<T> *m_top;
public:
SY() { nodesum = 0; m_top = NULL; }
virtual ~SY() { clear(); }
T& top() { return m_top->data; }
void add_node(node_S<T> *nd);
void push(T data);
int nodenum() { return nodesum; }
bool empty() { return nodesum == 0; }
void pop();
void clear();
};
template<class T>
void SY<T>::add_node(node_S<T>* nd) {
if (!empty())
nd->next = m_top;
m_top = nd;
nodesum++;
}
template<class T>
void SY<T>::push(T data) {
node_S<T> *ls = new node_S<T>(data);
add_node(ls);
}
template<class T>
void SY<T>::pop() {
if (empty()) return;
node_S<T> *de = m_top;
m_top = de->next;
delete de;
nodesum--;
}
template<class T>
void SY<T>::clear() {
if (empty()) return;
node_S<T> *de = m_top;
while (m_top->next != NULL) {
m_top = m_top->next;
delete de;
de = m_top;
} delete de;
nodesum = 0;
}