Advertisement
Guest User

Untitled

a guest
May 27th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. // list
  2. class Stack {
  3.     struct N {
  4.         Node* node;
  5.         N* next;
  6.  
  7.         N(Node* node, N* next) :
  8.             node(node),
  9.             next(next) {
  10.  
  11.         }
  12.         N() {
  13.             node = nullptr;
  14.             next = nullptr;
  15.         }
  16.     };
  17.  
  18. public:
  19.     N* top;
  20.     Stack() {
  21.         top = nullptr;
  22.     }
  23.     void push(Node* node) {
  24.         N* n = new N(node, top);
  25.         top = n; // assign new node as top
  26.     }
  27.     Node* pop() {
  28.         if (top == nullptr)
  29.             return nullptr;
  30.         else {
  31.             N* temp = top;
  32.             Node* node = temp->node;
  33.             top = temp->next;
  34.             // delete temp;
  35.             return node;
  36.         }
  37.     }
  38.     void print() {
  39.         N* temp = top;
  40.         while (temp) {
  41.             std::cout << (temp->node ? temp->node->data : -1) << " => ";
  42.             temp = temp->next;
  43.         }
  44.     }
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement