Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <class T>
  4. struct Node{
  5. T data_;
  6. Node<T> * next_;
  7. Node(const T& data, Node<T> * next = NULL) {
  8. data_ = data;
  9. next_ = next;
  10. }
  11. };
  12.  
  13. template <class T>
  14. class MyStack {
  15. Node<T> * first_;
  16. int count;
  17. public:
  18. MyStack();
  19. void push(const T& data);
  20. T pop();
  21. bool isEmpty() {
  22. return count == 0;
  23. }
  24. };
  25.  
  26. template <class T>
  27. MyStack<T>::MyStack(){
  28. count = 0;
  29. first_ = NULL;
  30. }
  31.  
  32. template <class T>
  33. void MyStack<T>::push(const T& data) {
  34. Node<T> * d = new Node<T>(data, first_);
  35. first_ = d;
  36. count++;
  37. }
  38.  
  39. template <class T>
  40. T MyStack<T>::pop() {
  41. T ret;
  42. if(first_) {
  43. Node<T> * tmp = first_ -> next_;
  44. ret = first_ -> data_;
  45. delete first_;
  46. first_ = tmp;
  47. count--;
  48. }
  49.  
  50. return ret;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement