Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. template <typename T>
  2.         class stack
  3.         {
  4.         public:
  5.  
  6.             stack()
  7.             {
  8.                 start = nullptr;
  9.             }
  10.  
  11.             stack(initializer_list<T> values)
  12.             {
  13.                 start = nullptr;
  14.                 for (auto val : values)
  15.                     push(val);
  16.             }
  17.  
  18.             void push(T val)
  19.             {
  20.                 if (start)
  21.                 {
  22.                     node* temp = new node(val);
  23.                     temp->next = start;
  24.                     start = temp;
  25.                 }
  26.                 else
  27.                     start = new node(val);
  28.             }
  29.  
  30.             void pop()
  31.             {
  32.                 node* temp = start->next;
  33.                 delete start;
  34.                 start = temp;
  35.             }
  36.  
  37.             ~stack()
  38.             {
  39.                 node* temp = start;
  40.                 while (temp != nullptr)
  41.                     pop();
  42.             }
  43.  
  44.             void print()
  45.             {
  46.                 node* temp = start;
  47.                 while (temp != nullptr)
  48.                     cout << temp->info << " ";
  49.                 cout << endl;
  50.             }
  51.  
  52.  
  53.         private:
  54.  
  55.             struct node
  56.             {
  57.                 node* next;
  58.                 T info;
  59.                
  60.                 node(T val)
  61.                 {
  62.                     info = val;
  63.                     next = nullptr;
  64.                 }
  65.             };
  66.  
  67.             node* start;
  68.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement