Pilha em c++

352 palavras 2 páginas
#include
#include

#define TAM 10

using namespace std;

struct pilha { int a[TAM]; int topo; int tamanho;
};

void iniciaPilha (struct pilha *p){ p->topo = -1; p->tamanho = TAM - 1;
}

bool pilhaVazia (struct pilha *p){ if (p->topo == -1) return true; else return false;
}

bool pilhaCheia (struct pilha *p){ if (p->topo == p-> tamanho) return true; else return false;
}

bool push (struct pilha *p, int n){ if (pilhaCheia(p)){ return false; } else { p->topo++; p->a[p->topo] = n; return true; }
}

bool pop (struct pilha *p, int *r){ if (pilhaVazia(p)){ return false; } else { *r = p->a[p->topo]; p->topo--; return true; }
}

void exibePilha (struct pilha *p){ int n; struct pilha t; iniciaPilha(&t); while (!pilhaVazia(p)){ pop(p,&n); push(&t,n); } cout << "\n\n"; while (!pilhaVazia(&t)){ pop(&t,&n); cout << n << " "; push(p,n); }
}

int main(int argc, char* argv[]){

struct pilha p; int x, op; iniciaPilha(&p); do { cout << "\n1 - Testa vazia"; cout << "\n2 - Testa cheia"; cout << "\n3 - Empilha"; cout << "\n4 - Desempilha"; cout << "\n5 - Exibir pilha"; cout << "\n6 - Sair"; cout << "\nDigite sua opcao: "; cin >> op; switch (op){ case 1: if (pilhaVazia(&p)){ cout << "Sim\n\n"; } else { cout << "Nao\n\n"; }break; case 2: if (pilhaCheia(&p)){ cout << "Sim\n\n"; } else { cout << "Nao\n\n"; }break; case 3: cout << "Digite um numero pra empilhar: "; cin >> x; if (push(&p,x)){ cout << "Ok\n\n"; } else { cout << "Erro\n\n"; }break; case 4: if (pop(&p,&x)){ cout << "Desempilhado = " << x <<"\n\n"; }

Relacionados

  • Pilhas C++
    1680 palavras | 7 páginas
  • Pilha em c/c++
    416 palavras | 2 páginas
  • PIlhas em C
    1268 palavras | 6 páginas
  • pilha c#
    1003 palavras | 5 páginas
  • Pilha c
    552 palavras | 3 páginas
  • Pilhas em c e c++
    476 palavras | 2 páginas
  • Pilhas em C
    758 palavras | 4 páginas
  • Pilha C++
    3832 palavras | 16 páginas
  • Pilha em C
    371 palavras | 2 páginas
  • Pilha ling c
    652 palavras | 3 páginas