Print values from a stack

Asked

Viewed 1,909 times

2

I have to make a code that prints all values of a stack in the opposite order in which they were inserted, only 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 class derived from the list Here is the code:

list. 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

pile. 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`

cell.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;
}
  • 1

    function returns would not be from {...} while (size != 0); ?

  • It was yes, thanks mine. But I’m having a problem that when the size reaches 0 the while hangs.

  • Programs in Portuguese get so ugly.

1 answer

1

The problem is this:

How you initialize the attribute size class pile as 1 and every time you insert it into the stack (push()) is incremented by 1 to the size attribute, so if you add 5 elements the size attribute will be 6. So, the DO-WHILE within the method retorna() should have stop condition (tamanho > 1).

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.