Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "PilhaEncad.h"
- #include "FilaEncad.h"
- using namespace std;
- int* inverte(int *vet, int n)
- {
- int* novo = NULL;
- novo = new int[n];
- for(int i=0;i<n;i++)
- novo[i] = vet[i];
- return novo;
- }
- FilaEncad* concatena(FilaEncad *f1, FilaEncad *f2)
- {
- FilaEncad* nova = NULL;
- nova = new FilaEncad;
- bool igual;
- for(No* p = f1->getPontini(); p->getProx()!= NULL; p = p->getProx())
- {
- nova->enfileira(f1->getInicio());
- f1->desenfileira();
- }
- for(No* p = f2->getPontini(); p->getProx() != NULL; p = p->getProx())
- {
- for(No* t = nova->getPontini(); p->getProx() != NULL; p = p->getProx())
- {
- if(p->getInfo() == t->getInfo())
- igual = true;
- }
- if(!igual)
- {
- nova->enfileira(f2->getInicio());
- f2->desenfileira();
- }
- igual = false;
- }
- return nova;
- }
- void removedaPilha(PilhaEncad* p, int x)
- {
- PilhaEncad aux;
- No* tmp = p->getPonttopo();
- while(tmp->getInfo()!= x && tmp != NULL)
- {
- aux.empilha(p->getTopo());
- p->desempilha();
- tmp = tmp->getProx();
- }
- if(p!=NULL)
- p->desempilha();
- tmp = aux.getPonttopo();
- while(tmp != NULL)
- {
- p->empilha(tmp->getInfo());
- aux.desempilha();
- tmp = tmp->getProx();
- }
- }
- int main()
- {
- /// TESTE COM PILHA
- PilhaEncad p;
- for(int i = 0; i < 5; i++) p.empilha(i);
- cout << "Pilha apos inserir 5 valores" << endl;
- p.imprime();
- for(int i = 20; i < 25; i++) p.empilha(i);
- cout << "Pilha apos inserir mais 5 valores" << endl;
- p.imprime();
- /// TESTE COM FILA
- FilaEncad f;
- for(int i=0; i<5; i++) f.enfileira(i);
- cout << "Fila apos inserir 5 valores" << endl;
- f.imprime();
- for(int i = 20; i < 25; i++) f.enfileira(i);
- cout << "Fila apos inserir mais 5 valores" << endl;
- f.imprime();
- return 0;
- }
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- #include <iostream>
- #include <cstdlib>
- #include "PilhaEncad.h"
- using namespace std;
- // construtor
- PilhaEncad::PilhaEncad()
- {
- cout << "Criando PilhaEncad" << endl;
- topo = NULL;
- n = 0;
- }
- // destrutor
- PilhaEncad::~PilhaEncad()
- {
- cout << "Destruindo PilhaEncad" << endl;
- No* p = topo;
- while(topo != NULL)
- {
- topo = p->getProx();
- delete p;
- p = topo;
- }
- }
- void PilhaEncad::empilha(int val)
- {
- No* p = new No();
- p->setInfo(val);
- p->setProx(topo);
- topo = p;
- n++;
- }
- int PilhaEncad::desempilha()
- {
- No* p;
- if(topo != NULL)
- {
- p = topo;
- topo = p->getProx();
- int val = p->getInfo();
- delete p;
- n --;
- return val;
- }
- else
- {
- cout << "ERRO: Pilha vazia!" << endl;
- exit(1);
- }
- }
- int PilhaEncad::getTopo()
- {
- if(topo != NULL)
- return topo->getInfo();
- else
- {
- cout << "Pilha vazia!" << endl;
- exit(1);
- }
- }
- bool PilhaEncad::vazia()
- {
- if(topo == NULL)
- return(true);
- else
- return(false);
- }
- void PilhaEncad::imprime()
- {
- if(vazia())
- return;
- for(No* t = topo; t != NULL; t = t->getProx() )
- {
- cout << t->getInfo() << "\t";
- }
- cout << "\n";
- }
- int PilhaEncad::tamanho()
- {
- return n;
- }
- No* PilhaEncad::getPonttopo()
- {
- return topo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement