Advertisement
Cherro

Anka #2

Apr 4th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class Node {
  4. public:
  5.     int data;
  6.     Node *next;
  7.     Node(int val, Node *next)
  8.     {
  9.         this->data = val;
  10.         this->next = next;
  11.     }
  12. };
  13.  
  14. class Stack {
  15. private:
  16.     Node * head;
  17. public:
  18.     Stack()
  19.     {
  20.         this->head = NULL;
  21.     }
  22.     void push(int data)
  23.     {
  24.         Node *new_Node = new Node(data,this->head);
  25.         this->head = new_Node;
  26.  
  27.     }
  28.     int pop()
  29.     {
  30.         if (this->head != NULL)
  31.         {
  32.             int tmp = this->head->data;
  33.             this->head = this->head->next;
  34.             return tmp;
  35.         }
  36.         return NULL;
  37.            
  38.  
  39.     }
  40. };
  41.  
  42.  
  43.  
  44. int main()
  45. {
  46.     Stack *st = new Stack();
  47.     for (int i = 0; i < 10; i++)
  48.         st->push(i);
  49.     int d = st->pop();
  50.     while (d)
  51.     {
  52.        
  53.         cout << d << endl;
  54.         d = st->pop();
  55.     }
  56.  
  57.  
  58.     getchar();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement