Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <class T>
- struct Node{
- T data_;
- Node<T> * next_;
- Node(const T& data, Node<T> * next = NULL) {
- data_ = data;
- next_ = next;
- }
- };
- template <class T>
- class MyStack {
- Node<T> * first_;
- int count;
- public:
- MyStack();
- void push(const T& data);
- T pop();
- bool isEmpty() {
- return count == 0;
- }
- };
- template <class T>
- MyStack<T>::MyStack(){
- count = 0;
- first_ = NULL;
- }
- template <class T>
- void MyStack<T>::push(const T& data) {
- Node<T> * d = new Node<T>(data, first_);
- first_ = d;
- count++;
- }
- template <class T>
- T MyStack<T>::pop() {
- T ret;
- if(first_) {
- Node<T> * tmp = first_ -> next_;
- ret = first_ -> data_;
- delete first_;
- first_ = tmp;
- count--;
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement