rihardmarius

pilas con listas

Nov 23rd, 2013
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct nodo {
  6.     int entry = 0;
  7.     nodo* next = nullptr;
  8. };
  9.  
  10. struct stack {
  11.     nodo* top = nullptr;
  12. };
  13.  
  14. bool is_stack_empty (stack& s)
  15. {
  16.     return s.top == nullptr;
  17. }
  18.  
  19. stack& push (stack& s, int v)
  20. {
  21.     nodo* nuevo = new nodo;
  22.     nuevo->entry = v;
  23.     nuevo->next = s.top;
  24.     s.top = nuevo;
  25.     return s;
  26. }
  27.  
  28. stack& pop (stack& s, int& v)
  29. {
  30.     nodo* old_top = s.top;
  31.     s.top = s.top->next;
  32.     v = old_top->entry;
  33.     delete old_top;
  34.     return s;
  35. }
  36.  
  37. void vaciar_stack (stack& s)
  38. {
  39.     int d;
  40.     while (not is_stack_empty(s))
  41.     {
  42.         pop(s,d);
  43.         cout << d << ' ';
  44.     }
  45.     cout << endl;
  46. }
RAW Paste Data