Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Pilha {
- private:
- int pilha[10];
- int ult;
- public:
- Pilha(); // construtor
- bool push(int valor); // inserir
- int pop(); // remover
- int top(); // retorna o ultimo item da pilha
- void print();
- };
- Pilha::Pilha(){
- for (int i=0; i<10; i++){
- pilha[i] = NULL;
- }
- ult = -1;
- }
- int Pilha::top(){
- return pilha[ult];
- }
- void Pilha::print(){
- for (int i=0; i <= ult; i++){
- cout << pilha[i] << " ";
- }
- cout << "\n";
- }
- bool Pilha::push(int valor){
- if (ult == 10) // verificando se a pilha esta cheia
- return false;
- ult++;
- pilha[ult] = valor;
- return true;
- }
- int Pilha::pop(){
- if (ult == -1) // pilha esta vazia
- return -1;
- int vlr_removido = pilha[ult];
- pilha[ult] = NULL;
- ult--;
- return vlr_removido;
- }
- int main()
- {
- Pilha p;
- p.push(9);
- p.push(8);
- p.push(7);
- p.push(6);
- p.push(5);
- p.print();
- p.push(4);
- p.push(3);
- p.push(2);
- p.push(1);
- p.print();
- cout << "\nValor removido da pilha: " << p.pop() << "\n";
- p.print();
- cout << "\nUltimo valor da pilha: " << p.top();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement