Advertisement
esquilo10

Untitled

Oct 4th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct no
  5. {
  6.     int info;
  7.     struct no *next;
  8. };
  9. typedef no* noptr;
  10.  
  11. void pushc (noptr &pilha, int x)
  12. {
  13.     noptr p;
  14.     p = new no;
  15.     p->info = x;
  16.     if (pilha == NULL) // ´e o primeiro n´o
  17.         pilha = p;
  18.     else
  19.         p->next = pilha->next; // j´a existem outros n´os
  20.     pilha->next = p;
  21. }
  22.  
  23. int popc (noptr &pilha, int &x)
  24. {
  25.     noptr p;
  26.     if (pilha == NULL) // a pilha j´a est´a vazia
  27.         return -1;
  28.     else // removendo n´o ininicial
  29.     {
  30.         p = pilha->next;
  31.         x = p->info;
  32.         if (p == pilha) // somente um n´o na pilha
  33.             pilha = NULL;
  34.         else // j´a existem outros n´os na pilha
  35.             pilha->next = p->next;
  36.         delete p;
  37.         return 0;
  38.     }
  39. }
  40.  
  41. void exibec(noptr &lista)
  42. {
  43.     noptr t = NULL;
  44.     noptr w = NULL;
  45.     pushc(w,lista->info);
  46.     for(t = lista->next; t != lista; t = t->next)
  47.     {
  48.         pushc(w,t->info);
  49.     }
  50.     cout << w->info << " ";
  51.     for(t = w->next; t != w; t = t->next)
  52.     {
  53.         cout << t->info << " ";
  54.     }
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60.     noptr pilha = NULL;
  61.     int val;//valor do nó
  62.  
  63.     cin >> val;
  64.     while(val != 0)
  65.     {
  66.         pushc(pilha, val);
  67.         cin >> val;
  68.     }
  69.     exibec(pilha);
  70.    
  71.         while(popc(pilha,val)==-1);
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement