Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. template <class T> class Stack
  2. {
  3. private:
  4.     struct Node
  5.     {
  6.         T value;
  7.         Node* prev;
  8.         Node *next;
  9.     };
  10.     Node *first;
  11.     Node *last;
  12. public:
  13.     Stack();
  14.     Stack(const Stack &L);
  15.     const Stack<T>& operator = (const Stack &L);
  16.     T pop();
  17.     void push(const T val);
  18.     int Size();
  19.     void clear();
  20.     ~Stack();
  21. };
  22.  
  23. template <class T> Stack<T>::Stack(void)
  24. {
  25.     first = NULL;
  26.     last = NULL;
  27. };
  28.  
  29. template <class T> Stack<T>::Stack(const Stack &L)
  30. {
  31.     first = NULL;
  32.     last = NULL;
  33.     Node *current = L.first;
  34.     Node *tmp;
  35.     while (current)
  36.     {
  37.         tmp = new Node;
  38.         if (first==NULL)
  39.             first = tmp;
  40.         else
  41.             last->next = tmp;
  42.         tmp->value = current->value;
  43.         tmp->next = NULL;
  44.         tmp->Prev = last;
  45.         last = tmp;
  46.         current = current->next;
  47.     }
  48. }
  49.  
  50. template <class T> const Stack<T>& Stack<T>::operator = (const Stack &L)
  51. {
  52.     first = NULL;
  53.     last = NULL;
  54.     Node *current = L.first;
  55.     Node *tmp;
  56.     while (current)
  57.     {
  58.         tmp = new Node;
  59.         if (first==NULL)
  60.             first = tmp;
  61.         else
  62.             last->next = tmp;
  63.         tmp->value = current->value;
  64.         tmp->next = NULL;
  65.         tmp->Prev = last;
  66.         last = tmp;
  67.         current = current->next;
  68.     }
  69.     return *this;
  70. }
  71. template <class T> T Stack<T>::pop()
  72. {
  73.     T val = last->value;
  74.     last = last->prev;
  75.     return val;
  76. }
  77. template <class T> void Stack<T>::push(const T val)
  78. {
  79.     Node *tmp = new Node;
  80.     if (first==NULL)
  81.         first = tmp;
  82.     else
  83.         last->next = tmp;
  84.     tmp->value = val;
  85.     tmp->next = NULL;
  86.     tmp->prev = last;
  87.     last = tmp;
  88. };
  89.  
  90. template <class T> void Stack<T>::clear()
  91. {
  92.     while (first!=NULL)
  93.     {
  94.         Node *tmp = first;
  95.         first = first->next;
  96.         delete tmp;
  97.     }
  98. };
  99.  
  100. template <class T> Stack<T>::~Stack()
  101. {
  102.     clear();
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement