Print the values in a stack

2

I have to make a code that prints the all values of a stack in the opposite order in which they were inserted, except that I can only print one value at a time, each time I want to see the previous value I have to select option 8 in my program.

Note: My stack is a derived class from the list Here is the code:

lista.h

#include <string>
#include<iostream>
using namespace std;

struct Nodo //nodos para listas duplamente encadeadas
{
    string valor;
    Nodo *anterior;
    Nodo *proximo;
};

#ifndef LISTA_H
#define LISTA_H
class Lista
{
public:
    Lista(){};
    Lista(Nodo *primeiro);

    void insereFim(Nodo *novo);
    void insereInicio(Nodo *novo);
    void insereAntes(Nodo *novo, Nodo *ref);
    void insereDepois(Nodo *novo, Nodo *ref);
    void removeInicio();
    void removeFim();
    void removeEscolhe(Nodo *n);
    void vetore (Nodo*n, int ref);

protected:
    Nodo *inicio;
    Nodo *fim;
    int tamanho;
    int vetor;

};

#endif

stack.h

#include"lista.h"
#ifndef PILHA_H
#define PILHA_H

class pilha 
{
public:
    pilha(){};
    pilha(Nodo *p);
    void push(Nodo *novo); //insercao
    void pop(); //remocao
    Nodo *top(); //tamanho
    void retorna();
    //bool empty();

private:
    Nodo *topo;
    int tamanho;
};

#endif PILHA_H'

stack.cpp

#include"pilhas.h"

pilha::pilha(Nodo *p)
{
    p->anterior = NULL;
    p->proximo = NULL;

    topo = p;
    tamanho = 1;
};

Nodo *pilha::top()
{
    return topo;
};

void pilha::push(Nodo *novo)
{    
    novo ->proximo =NULL;
    novo->anterior =topo;    

    topo = novo;
    tamanho++;
};

void pilha::pop()
{
    if(tamanho == 0)

        cout << "Impossivel remover";
    else if(tamanho ==1)
    {
        topo =NULL;
        tamanho=0;
    }
    else
    {
        topo = topo -> anterior;
        tamanho--;
    }
};

void pilha::retorna()
{
    if(tamanho == 0)
    {
        cout << "Não há caminho" << endl;
    }
    else if(tamanho <= 1) 
    {

        do{

            cout << "   " << topo->valor << endl; 
            topo = topo -> anterior;
            tamanho--;

        }while(tamanho==NULL);

    }
};

Main .cpp

#include"pilhas.h"

int main()
{
    //Criando as 4 salas
    Nodo n1, n2, n3 , n4;
    pilha Salas;
    int N;
    bool S=false;
    n1.valor="Calabouco";
    n2.valor="Hall";
    n3.valor="Quartos";
    n4.valor="Cozinha";

    //Pergunta pra onde quer ir
    while (!S)
    {
        cout << "Escolha um local " <<endl;
        cout << "1-Calabouco" << endl;
        cout << "2-Hall" <<endl;
        cout << "3-Quartos" << endl;
        cout << "4-Cozinha" << endl;
        cout << "7-Sair" << endl;
        cout << "8-Mostrar caminho de Volta" << endl;
        cin >> N;

        switch (N)
        {

        case 1:
            Salas.push(&n1);
            cout << n1.valor << endl;

            break;
        case 2:
            Salas.push(&n2);
            cout << n2.valor << endl;
            break;
        case 3:
            Salas.push(&n3);
            cout << n3.valor << endl;
            break;
        case 4:
            Salas.push(&n4);
            cout << n4.valor << endl;
            break;
        case 7:
            S=true;
            break;
        case 8://Saida 
            Salas.retorna();

            break;
        default:
            cout << "Essa sala nao existe" << endl;
            break;
        }
    }    
    system("pause");
    return 0;
}
    
asked by anonymous 24.10.2014 / 20:25

1 answer

1

The problem is as follows:

As you initialize the size attribute of the stack class to 1, and whenever you insert into the stack ( push() ) is incremented plus 1 to the size attribute, then you add 5 elements the size attribute will equal 6. Therefore, the DO-WHILE within the retorna() method should have stop condition ( tamanho > 1 ).

Another solution is to initialize the size with 0 and put the stop condition as ( tamanho > 0 ), which I believe to be more prudent since the stack starts empty.

    
20.02.2015 / 18:13