Commit 46840fc0 by Alan de Oliveira

Iniciando Atividades

parents
#include <iostream>
#include <unistd.h>
using namespace std;
template <class D> class container {
public:
container *proximo;
D elemento;
};
template <class T, class Container=container<T>>
class deque {
public:
void insereInicio(T elem) {
auto cont = new container<T>();
cont->elemento = elem;
if (inicio == nullptr) {
fim = cont;
cont->proximo = nullptr;
}
else
cont->proximo = inicio;
inicio = cont;
}
void insereFim(T elem) {
auto cont = new container<T>();
cont->elemento = elem;
cont->proximo = nullptr;
if (fim == nullptr)
inicio = cont;
else
fim->proximo = cont;
fim = cont;
}
T removeinicio() {
if (inicio != nullptr) {
T elemento = inicio->elemento;
Container *i = inicio;
if (inicio == fim) {
inicio = nullptr;
fim = nullptr;
}
else
inicio = inicio->proximo;
delete i;
return elemento;
}
else
return -1;
}
T removeFim() {
if (fim != nullptr) {
T elemento = fim->elemento;
if (inicio == fim) {
inicio = nullptr;
delete fim;
fim = nullptr;
}
else {
Container *f = fim;
Container *novoFim = inicio;
while (novoFim->proximo != fim){
novoFim = novoFim->proximo;
}
fim = novoFim;
delete f;
}
return elemento;
}
else
return -1;
}
T bucaInicio() {
return nullptr;
}
T buscaFim() {
return nullptr;
}
private:
Container *inicio = nullptr;
Container *fim = nullptr;
};
int main(int argc, char* argv[]) {
deque<int> meuDeque;
meuDeque.insereInicio(9);
meuDeque.insereInicio(3);
meuDeque.insereInicio(4);
meuDeque.insereInicio(7);
auto d = meuDeque.removeFim();
/*printf ("primeiro %d ", d);
d = meuDeque.removeFim();
printf ("segundo %d ", d);
d = meuDeque.removeFim();
printf ("terceiro %d ", d);
d = meuDeque.removeinicio();
printf ("quarto %d ", d);
d = meuDeque.removeinicio();*/
while (d != -1) {
printf ("%d ", d);
d = meuDeque.removeinicio();
}
return 0;
}
\ No newline at end of file
#include <iostream>
#include <unistd.h>
using namespace std;
template <class D> class container {
public:
container *proximo;
D elemento;
};
template <class T, class Container=container<T>>
class Fila {
public:
void enfilera(T elem) {
auto cont = new container<T>();
cont->elemento = elem;
if (fim != nullptr)
fim->proximo = cont;
else
inicio = cont;
fim = cont;
}
T desenfilera() {
if (inicio != nullptr) {
T elemento = inicio->elemento;
Container *i = inicio;
inicio = inicio->proximo;
if (inicio == nullptr)
fim = nullptr;
delete i;
return elemento;
}
else
return -1;
}
T frente() {
if (inicio != nullptr)
return inicio->elemento;
else
return -1;
}
private:
Container *inicio = nullptr;
Container *fim = nullptr;
};
template <class T>
class Pilha {
public:
void empilha(T elemento) {
if (fila == nullptr)
fila = new Fila<T>();
fila->enfilera(elemento);
}
T desempilha() {
if (fila->frente() == -1)
return -1;
auto filaAux = new Fila<T>();
T elemento;
while (fila->frente() != -1) {
elemento = fila->desenfilera();
if (fila->frente() != -1)
filaAux->enfilera(elemento);
}
delete fila;
fila = filaAux;
return elemento;
}
private:
Fila<T> *fila = new Fila<T>();
};
int main(int argc, char* argv[]) {
Pilha<int> mpilha;
mpilha.empilha(2);
mpilha.empilha(32);
mpilha.empilha(54);
mpilha.empilha(96);
mpilha.empilha(11);
mpilha.empilha(78);
int e = mpilha.desempilha();
while (e != -1) {
cout << "elem: " << e <<endl;
e = mpilha.desempilha();
}
return 0;
}
\ No newline at end of file
#include <iostream>
#include <unistd.h>
using namespace std;
template <class T>
class Pilha {
public:
void empilha(T elemento) {
N++;
elementos[N -1] = elemento;
}
T desempilha() {
if (N > 0) {
N--;
return elementos[N];
}
else
return -1;
}
T topo() {
if (N > 0)
return elementos[N -1];
else
return -1;
}
private:
static const int MAX = 900;
int N = 0;
T elementos[MAX];
};
template <class T>
class Fila {
public:
void enfilera(T elem) {
pilha->empilha(elem);
}
T desenfilera() {
if (pilha->topo() == -1)
return -1;
auto paux = new Pilha<T>();
T elemento;
while (pilha->topo() != -1) {
cout << " " << pilha->topo();
elemento = pilha->desempilha();
if (pilha->topo() != -1)
paux->empilha(elemento);
}
cout << endl;
while (paux-> topo() != -1) {
pilha->empilha(paux->desempilha());
}
delete paux;
return elemento;
}
T frente() {
pilha->topo();
}
private:
Pilha<T> *pilha = new Pilha<T>();
};
int main(int argc, char* argv[]) {
Fila<int> fila;
fila.enfilera(2);
fila.enfilera(32);
fila.enfilera(54);
fila.enfilera(96);
fila.enfilera(11);
fila.enfilera(78);
int e = fila.desenfilera();
while (e != -1) {
cout << "---------------- elemento desinfilerado: " << e << "------------" << endl;
e = fila.desenfilera();
}
return 0;
}
\ No newline at end of file
#include <iostream>
template <class D> class container {
public:
container *proximo;
D elemento;
};
template <class T, class Container=container<T>>
class Fila {
public:
void enfilera(T elem) {
auto cont = new container<T>();
cont->elemento = elem;
if (fim != nullptr)
fim->proximo = cont;
else
inicio = cont;
fim = cont;
}
T desenfilera() {
if (inicio != nullptr) {
T elemento = inicio->elemento;
Container *i = inicio;
inicio = inicio->proximo;
if (inicio == nullptr)
fim = nullptr;
delete i;
return elemento;
}
else
return -1;
}
T frente() {
if (inicio != nullptr)
return inicio->elemento;
else
return -1;
}
private:
Container *inicio = nullptr;
Container *fim = nullptr;
};
template <class T>
class Pilha {
public:
void empilha(T elemento) {
N++;
elementos[N -1] = elemento;
}
T desempilha() {
if (N > 0) {
N--;
return elementos[N];
}
else
return -1;
}
T topo() {
if (N > 0)
return elementos[N -1];
else
return -1;
}
private:
static const int MAX = 900;
int N = 0;
T elementos[MAX];
};
template <class T> Pilha<T> inverterPilha(Pilha<T> pilha) {
Pilha<T> pilhaAux;
while (pilha.topo() != -1) {
pilhaAux.empilha(pilha.desempilha());
}
return pilhaAux;
}
template <class T> Pilha<T> inverterPilhaComFila(Pilha<T> pilha) {
Fila<T> filaAux;
while (pilha.topo() != -1) {
filaAux.enfilera(pilha.desempilha());
}
while (filaAux.frente() != -1) {
pilha.empilha(filaAux.desenfilera());
}
return pilha;
}
template <class T> Pilha<T> inverterPilhaComDuasPilhas(Pilha<T> pilha) {
Pilha<T> pilhaAux, pilhaAux2;
while (pilha.topo() != -1) {
pilhaAux.empilha(pilha.desempilha());
}
while (pilhaAux.topo() != -1) {
pilhaAux2.empilha(pilhaAux.desempilha());
}
while (pilhaAux2.topo() != -1) {
pilhaAux.empilha(pilhaAux2.desempilha());
}
return pilhaAux;
}
void preencherPilha(Pilha<char> &pilha) {
pilha.empilha('a');
pilha.empilha('b');
pilha.empilha('c');
pilha.empilha('d');
pilha.empilha('e');
pilha.empilha('f');
pilha.empilha('g');
pilha.empilha('h');
pilha.empilha('i');
pilha.empilha('j');
pilha.empilha('k');
pilha.empilha('l');
pilha.empilha('m');
pilha.empilha('n');
pilha.empilha('o');
pilha.empilha('p');
pilha.empilha('q');
pilha.empilha('r');
pilha.empilha('s');
pilha.empilha('t');
pilha.empilha('u');
pilha.empilha('v');
pilha.empilha('w');
pilha.empilha('x');
pilha.empilha('y');
pilha.empilha('z');
}
void printarPilha(Pilha<char> &pilha, std::string texto) {
std::cout << "----" << texto << "------" << std::endl;
while (pilha.topo() != -1) {
std::cout << "| " << pilha.desempilha() << " " ;
}
std::cout << "|" << std::endl;
}
int main(int argc, char* argv[]) {
Pilha<char> pilha;
preencherPilha(pilha);
printarPilha(pilha, "Pilha normal");
preencherPilha(pilha);
pilha = inverterPilha(pilha);
printarPilha(pilha, "Pilha invertida com pilha");
preencherPilha(pilha);
pilha = inverterPilhaComFila(pilha);
printarPilha(pilha, "Pilha invertida com FILA");
preencherPilha(pilha);
pilha = inverterPilhaComDuasPilhas(pilha);
printarPilha(pilha, "Pilha invertida com duas Pilhas");
return 0;
}
\ No newline at end of file
#include <iostream>
template <class D> class container {
public:
container *proximo;
D elemento;
};
template <class T, class Container=container<T>>
class Fila {
public:
void enfilera(T elem) {
auto cont = new container<T>();
cont->elemento = elem;
if (fim != nullptr)
fim->proximo = cont;
else
inicio = cont;
fim = cont;
}
T desenfilera() {
if (inicio != nullptr) {
T elemento = inicio->elemento;
Container *i = inicio;
inicio = inicio->proximo;
if (inicio == nullptr)
fim = nullptr;
delete i;
return elemento;
}
else
return -1;
}
T frente() {
if (inicio != nullptr)
return inicio->elemento;
else
return -1;
}
private:
Container *inicio = nullptr;
Container *fim = nullptr;
};
template <class T>
class Pilha {
public:
void empilha(T elemento) {
N++;
elementos[N -1] = elemento;
}
T desempilha() {
if (N > 0) {
N--;
return elementos[N];
}
else
return -1;
}
T topo() {
if (N > 0)
return elementos[N -1];
else
return -1;
}
private:
static const int MAX = 900;
int N = 0;
T elementos[MAX];
};
template <class T> Fila<T> inverterFila(Fila<T> fila) {
Fila<T> filaAux;
while (fila.frente() != -1) {
filaAux.enfilera(fila.desenfilera());
}
return filaAux;
}
template <class T> Fila<T> inverterFilaComPilha(Fila<T> fila) {
Pilha<T> pilha;
while (fila.frente() != -1) {
pilha.empilha(fila.desenfilera());
}
printarPilha(pilha, "Fila invertida com Pilha");
return fila;
}
template <class T> Pilha<T> inverterPilhaComDuasPilhas(Pilha<T> pilha) {
Pilha<T> pilhaAux, pilhaAux2;
while (pilha.topo() != -1) {
pilhaAux.empilha(pilha.desempilha());
}
while (pilhaAux.topo() != -1) {
pilhaAux2.empilha(pilhaAux.desempilha());
}
while (pilhaAux2.topo() != -1) {
pilhaAux.empilha(pilhaAux2.desempilha());
}
return pilhaAux;
}
void preencherFila(Fila<char> &fila) {
fila.enfilera('a');
fila.enfilera('b');
fila.enfilera('c');
fila.enfilera('d');
fila.enfilera('e');
fila.enfilera('f');
fila.enfilera('g');
fila.enfilera('h');
fila.enfilera('i');
fila.enfilera('j');
fila.enfilera('k');
fila.enfilera('l');
fila.enfilera('m');
fila.enfilera('n');
fila.enfilera('o');
fila.enfilera('p');
fila.enfilera('q');
fila.enfilera('r');
fila.enfilera('s');
fila.enfilera('t');
fila.enfilera('u');
fila.enfilera('v');
fila.enfilera('w');
fila.enfilera('x');
fila.enfilera('y');
fila.enfilera('z');
}
void printarFila(Fila<char> &fila, std::string texto) {
std::cout << "----" << texto << "------" << std::endl;
while (fila.frente() != -1) {
std::cout << "| " << fila.desenfilera() << " " ;
}
std::cout << "|" << std::endl;
}
void printarPilha(Pilha<char> &pilha, std::string texto) {
std::cout << "----" << texto << "------" << std::endl;
while (pilha.topo() != -1) {
std::cout << "| " << pilha.desempilha() << " " ;
}
std::cout << "|" << std::endl;
}
int main(int argc, char* argv[]) {
Fila<char> fila;
preencherFila(fila);
printarFila(fila, "Fila normal");
preencherFila(fila);
fila = inverterFila(fila);
printarFila(fila, "Fila invertida com outra fila ???");
preencherFila(fila);
inverterFilaComPilha(fila);
//printarFila(fila, "Fila invertida com Pilha");
/*
reencherFila(fila);
pilha = inverterPilhaComFila(pilha);
printarFila(fila, "Fila normal");
reencherFila(fila);
pilha = inverterPilhaComDuasPilhas(pilha);
printarFila(fila, "Fila normal");*/
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment