1. 结构体
![](https://i-blog.csdnimg.cn/blog_migrate/e4f63204097a426a9847944abdcb4517.png)
2. 初始化
![](https://i-blog.csdnimg.cn/blog_migrate/0749802e8581d3de3c75415a8e9a1dc0.png)
3. 入栈
![](https://i-blog.csdnimg.cn/blog_migrate/fd614549a1b07fb145dc9d04b6b5f8c1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bbf5569b32f4d2059d08d53c353c33af.png)
4. 出栈
![](https://i-blog.csdnimg.cn/blog_migrate/040828e7aabe77b89b4e3edaf90b2b6c.png)
#include<iostream>
using namespace std;
template<class T>
class linknode {
public:
//linknode() { cout << endl; };
public:
T data;
linknode<T>* next;
};
template<class T>
class Lstack {
public:
linknode<T> header;
int m_size;
public:
Lstack() {
header.next = NULL;
this->m_size = 0;
}
void push(const T& data) {
linknode<T>* mystack = &header;
linknode<T>* current = new linknode<T>;
current->data = data;
current->next = mystack->next;
mystack->next = current;
this->m_size++;
}
void pop() {
linknode<T>* mystack = &header;
linknode<T>* next_node = mystack->next;
mystack->next = next_node->next;
this->m_size--;
}
linknode<T>* top() {
linknode<T>* mystack = &header;
return mystack->next;
}
};
int main() {
Lstack<int> listStack;
listStack.push(1);
listStack.push(2);
listStack.push(3);
linknode<int>* top_data = listStack.top();
cout << top_data->data << endl;
cout << "---------------------"<<endl;
listStack.pop();
top_data = listStack.top();
cout << top_data->data << endl;
return 0;
}