Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
#ifndef PILHA_H_INCLUDED #define PILHA_H_INCLUDED using namespace std; struct Node{ int info; Node *prox; }; struct Pilha{ Node *topo; Pilha(); bool empilha(int x); int desempilha(); int elementodotopo(); bool pilhavazia(); void mostrapilha();//percorre cada um dos elementos }; Pilha::Pilha(){ topo=NULL;//topo=null,pilha vazia } bool Pilha::pilhavazia(){ return topo==NULL; } bool Pilha::empilha(int x){ bool t=true; Node *aux = new Node;//pedido de espaço de memoria ao SO, new aloca //ref de new = -> if (aux==NULL)//se nao conseguir o espaço t = false; else{ aux->info=x; aux->prox=topo; topo=aux; } return t;//retorna t se conseguir inserir elemento na pilha } //topo-null,pilha vazia int Pilha::elementodotopo(){ return topo->info; } int Pilha::desempilha(){//somente se a pilha nao estiver vazia int t=topo->info; //nao usa new pq nao aloca Node *aux = topo;//variavel aux aponta para topo topo = topo->prox; delete aux; return t; } void Pilha::mostrapilha(){ Node *aux = topo; while (aux != NULL){ cout<<aux->info<<endl; aux = aux->prox; } } #endif // PILHA_H_INCLUDED
Compartilhar