Pilhas
Autor: Jose Victor Cardoso | Publicado: 2010-04-21T12:06:00-07:00

Pilhas são estruturas de dados que visam armazenar elementos em termos do seguinte conceito: o ultimo elemento inserido é o primeiro elemento que será removido, são conhecidas como estruturas LIFO ( last in, first out ). São denominadas em analogia a pilhas de pratos ou livros, que para não corromper a estrutura é preciso tirar o primeiro elemento e assim por diante, até a pilha esvaziar. Para elucidar como as pilhas funcionam, e ententer como a sua lógica pode ser usufruia na confecção de programas para resolver determinados problemas&nbsp; segue um codigo em C++, que utiliza de pilhas para converter numeros em decimais em numeros de base 2 a 9:

<![CDATA[
// Autor: Jose Victor Leao Cardoso
// Converte decimais para numeros de base 2 a 9
#include 
using namespace std;

class NoPilha
{
&nbsp;&nbsp;&nbsp; public:
&nbsp;&nbsp;&nbsp;&nbsp; int numero;
&nbsp;&nbsp;&nbsp;&nbsp; NoPilha *prox;
&nbsp;&nbsp;&nbsp;&nbsp; NoPilha( int num, NoPilha *p );
};

NoPilha::NoPilha( int num, NoPilha *p)
{
&nbsp;&nbsp;&nbsp; numero = num;
&nbsp;&nbsp;&nbsp; prox = p;
}

class Pilha
{
&nbsp;&nbsp;&nbsp; private:
&nbsp;&nbsp;&nbsp;&nbsp; NoPilha *topo;

&nbsp;&nbsp;&nbsp; public:
&nbsp;&nbsp;&nbsp;&nbsp; Pilha() { topo = NULL; }
&nbsp;&nbsp;&nbsp;&nbsp; ~Pilha();
&nbsp;&nbsp;&nbsp;&nbsp; void Push( int num );
&nbsp;&nbsp;&nbsp;&nbsp; int Pop();
&nbsp;&nbsp;&nbsp;&nbsp; int Top();
&nbsp;&nbsp;&nbsp;&nbsp; int Empty();
};

int Pilha::Empty()
{
&nbsp;&nbsp;&nbsp; if( topo == NULL )
&nbsp;&nbsp;&nbsp;&nbsp; return 1;
&nbsp;&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;
}

void Pilha::Push( int num )
{
&nbsp;&nbsp;&nbsp; NoPilha *novo;
&nbsp;&nbsp;&nbsp; novo = new NoPilha(num, topo);
&nbsp;&nbsp;&nbsp; topo = novo;
}

int Pilha::Pop()
{
&nbsp;&nbsp;&nbsp; if( Empty() )
&nbsp;&nbsp;&nbsp; return -1; // pilha está vazia

&nbsp;&nbsp;&nbsp; else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NoPilha *aux;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aux = topo;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; topo = topo->prox;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete aux;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; return 1;
}

int Pilha::Top()
{
&nbsp;&nbsp;&nbsp; if(!Empty())
&nbsp;&nbsp;&nbsp; return (topo->numero);
}

Pilha::~Pilha()
{
&nbsp;&nbsp;&nbsp; while( !Empty() )
&nbsp;&nbsp;&nbsp;&nbsp; Pop();
}

/* Funcao que converte os numeros divindo o decimal pela base
sucessivamente ate encontrar o valor final da conversão */
void converte( int n, int b, Pilha *pilha )
{
&nbsp;&nbsp;&nbsp; int resto, quociente = 1;

&nbsp;&nbsp;&nbsp; while( quociente != 0 )
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; resto = n%b;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pilha->Push(resto); // usa-se pilha para armazenar os restos das divisões
/* pilha neste problema é util porque a retira no sentido inverso resulta
na resposta esperada */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout <<"\nresto: " << resto ;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quociente = n/b;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout <<"\nquociente:"<< quociente << endl;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = quociente;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout << " \ntop: "<< pilha->Top();
&nbsp;&nbsp;&nbsp; }
}

int main()
{
&nbsp;&nbsp;&nbsp; Pilha result;
&nbsp;&nbsp;&nbsp; int num, base;
&nbsp;&nbsp;&nbsp; char op;

&nbsp;&nbsp;&nbsp; cout << "Converte decimal para numero de base 2 a 9" << endl;

&nbsp;&nbsp;&nbsp; do{
&nbsp;&nbsp;&nbsp; cout << "Entre com o numero e a base alvo de conversao: " << endl;
&nbsp;&nbsp;&nbsp; cin >> num >> base;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; converte(num, base, &amp;result);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout <<"\n" <<< " na base " << base << " corresponde a: " ;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while( !result.Empty() ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout << result.Top();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.Pop();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout << "\n\nDigite qualquer tecla para continuar ou 's' para sair: ";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cin >> op;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; system("cls");
&nbsp;&nbsp;&nbsp; }while( op != 's' &amp;&amp; op != 'S' );

&nbsp;&nbsp;&nbsp; return 0;
}