Advertisement
Guest User

fila/pilha.cpp

a guest
Oct 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include "PilhaEncad.h"
  3. #include "FilaEncad.h"
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int* inverte(int *vet, int n)
  9. {
  10.     int* novo = NULL;
  11.     novo = new int[n];
  12.  
  13.     for(int i=0;i<n;i++)
  14.         novo[i] = vet[i];
  15.  
  16.     return novo;
  17.  
  18. }
  19.  
  20. FilaEncad* concatena(FilaEncad *f1, FilaEncad *f2)
  21. {
  22.     FilaEncad* nova = NULL;
  23.     nova = new FilaEncad;
  24.     bool igual;
  25.     for(No* p = f1->getPontini(); p->getProx()!= NULL; p = p->getProx())
  26.     {
  27.         nova->enfileira(f1->getInicio());
  28.         f1->desenfileira();
  29.     }
  30.     for(No* p = f2->getPontini(); p->getProx() != NULL; p = p->getProx())
  31.     {
  32.         for(No* t = nova->getPontini(); p->getProx() != NULL; p = p->getProx())
  33.         {
  34.             if(p->getInfo() == t->getInfo())
  35.                 igual = true;
  36.         }
  37.         if(!igual)
  38.         {
  39.             nova->enfileira(f2->getInicio());
  40.             f2->desenfileira();
  41.         }
  42.         igual = false;
  43.     }
  44.  
  45.     return nova;
  46. }
  47.  
  48. void removedaPilha(PilhaEncad* p, int x)
  49. {
  50.     PilhaEncad aux;
  51.     No* tmp = p->getPonttopo();
  52.     while(tmp->getInfo()!= x && tmp != NULL)
  53.     {
  54.         aux.empilha(p->getTopo());
  55.         p->desempilha();
  56.         tmp = tmp->getProx();
  57.     }
  58.     if(p!=NULL)
  59.         p->desempilha();
  60.     tmp = aux.getPonttopo();
  61.     while(tmp != NULL)
  62.     {
  63.         p->empilha(tmp->getInfo());
  64.         aux.desempilha();
  65.         tmp = tmp->getProx();
  66.     }
  67. }
  68. int main()
  69. {
  70.     /// TESTE COM PILHA
  71.     PilhaEncad p;
  72.  
  73.     for(int i = 0; i < 5; i++) p.empilha(i);
  74.     cout << "Pilha apos inserir 5 valores" << endl;
  75.    p.imprime();
  76.  
  77.     for(int i = 20; i < 25; i++) p.empilha(i);
  78.     cout << "Pilha apos inserir mais 5 valores" << endl;
  79.  p.imprime();
  80.  
  81.     /// TESTE COM FILA
  82.  
  83.     FilaEncad f;
  84.     for(int i=0; i<5; i++) f.enfileira(i);
  85.     cout << "Fila apos inserir 5 valores" << endl;
  86.     f.imprime();
  87.  
  88.     for(int i = 20; i < 25; i++) f.enfileira(i);
  89.     cout << "Fila apos inserir mais 5 valores" << endl;
  90.     f.imprime();
  91.  
  92.     return 0;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  100.  
  101.  
  102.  
  103. #include <iostream>
  104. #include <cstdlib>
  105. #include "PilhaEncad.h"
  106.  
  107. using namespace std;
  108.  
  109. // construtor
  110. PilhaEncad::PilhaEncad()
  111. {
  112.     cout << "Criando PilhaEncad" << endl;
  113.     topo = NULL;
  114.     n = 0;
  115. }
  116.  
  117. // destrutor
  118. PilhaEncad::~PilhaEncad()
  119. {
  120.     cout << "Destruindo PilhaEncad" << endl;
  121.     No* p = topo;
  122.     while(topo != NULL)
  123.     {
  124.         topo = p->getProx();
  125.         delete p;
  126.         p = topo;
  127.     }
  128.  
  129. }
  130.  
  131. void PilhaEncad::empilha(int val)
  132. {
  133.     No* p = new No();
  134.     p->setInfo(val);
  135.     p->setProx(topo);
  136.     topo = p;
  137.     n++;
  138. }
  139.  
  140. int PilhaEncad::desempilha()
  141. {
  142.     No* p;
  143.     if(topo != NULL)
  144.     {
  145.         p = topo;
  146.         topo = p->getProx();
  147.         int val = p->getInfo();
  148.         delete p;
  149.         n --;
  150.         return val;
  151.     }
  152.     else
  153.     {
  154.         cout << "ERRO: Pilha vazia!" << endl;
  155.         exit(1);
  156.     }
  157. }
  158.  
  159. int PilhaEncad::getTopo()
  160. {
  161.     if(topo != NULL)
  162.         return topo->getInfo();
  163.     else
  164.     {
  165.         cout << "Pilha vazia!" << endl;
  166.         exit(1);
  167.     }
  168. }
  169.  
  170. bool PilhaEncad::vazia()
  171. {
  172.     if(topo == NULL)
  173.         return(true);
  174.     else
  175.         return(false);
  176. }
  177.  
  178. void PilhaEncad::imprime()
  179. {
  180.     if(vazia())
  181.         return;
  182.  
  183.     for(No* t = topo; t != NULL; t = t->getProx() )
  184.     {
  185.         cout << t->getInfo() << "\t";
  186.     }
  187.     cout << "\n";
  188.  
  189. }
  190.  
  191. int PilhaEncad::tamanho()
  192. {
  193.     return n;
  194. }
  195.  
  196. No* PilhaEncad::getPonttopo()
  197. {
  198.     return topo;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement