Print content of custom-type LIFO stack

3

I have the next class;

class Estudiante {

private:
    string nombre,cedula;
    int matricula;
    float calificacion;

public:
    //se dejo con un destructor por defecto
    void anadir(int _matricula,string _nombre,string _cedula,float _calificacion);
    void insertar();
    void borrar();
    void listar();
    void listar_inverso();
    void promedio();
    int getMatricula() const {
        return matricula;
    }
    float getCalificacion() const {
        return calificacion;
    }

};
/**
* Este metodo es utilizado para asignarle los datos a cada una de las propiedades
* de esta manera el objeto con todas sus propiedades asignadas se almacenan
* en el vector de estudiantes
**/
void Estudiante::anadir(int _matricula,string _nombre,string _cedula,float _calificacion) {
    matricula = _matricula;
    nombre = _nombre;
    cedula = _cedula;
    calificacion = _calificacion;
}
/**
* Lista los estudiantes, es utilizado por el vecto junto a un
* iterador
**/
void Estudiante::listar() {
    cout << matricula <<" "  << nombre <<" " << cedula <<" " << calificacion << endl;

}

I use it to create objects of type estudiante and save them in a stack of the type of the class;

stack<Estudiante> lista_estudiantes;

Estudiante *est;
//Nuevo objeto de tipo estudiante
est = new Estudiante;

est->anadir(100,"estudent1","000-000000-0",88);
lista_estudiantes.push(*est);
est->anadir(200,"estudent2","000-000000-0",80);
lista_estudiantes.push(*est);
est->anadir(300,"estudent3","000-000000-0",95);
lista_estudiantes.push(*est);

I have tried the following;

for ( int it = 0; it < lista_estudiantes.size(); ++it ){
                    est->listar();
                }

But in this case I only get the last element created, not all, if I have 3 elements created I printed 3 times the last one;

MATRICULA |   NOMBRE  |    CEDULA    | CALIFICACION |
300         estudent3   000-000000-0        95
300         estudent3   000-000000-0        95
300         estudent3   000-000000-0        95

All this compiling works, runs and I can register estudiantes , now I can not print the contents of my stack.

    
asked by Albert Hidalgo 30.09.2018 в 18:42
source

1 answer

2
  

But in this case I only get the last element created, not all, if I have 3 elements created I printed 3 times the last one;

Seen your code is the expected result, you do not have to be surprised by it. Notice that in the loop, only it is going to change ... the pointer est is the same all the time:

for ( int it = 0; it < lista_estudiantes.size(); ++it ){
  est->listar();
}

On the other hand, in a stack it is not possible to iterate through its elements (that is why it is a stack and not a vector). To recover your items you can choose to change stack by vector or empty the stack (putting your items in another container) ... there are no mechanisms to iterate batteries.

Unless the use of the stack is a requirement I would choose to use std::vector instead of std::stack . This container has functions that allow it to emulate the behavior of a LIFO stack:

  • push_back : Add an item to the end of the list
  • pop_back : Remove the last item from the list
  • back : Gets a reference to the last item in the list

And besides, since it is not a stack, it is possible to iterate its elements:

// Iteracion por indices
for ( int i= 0; i< lista_estudiantes.size(); ++i){
  lista_estudiantes[i].listar();
}

// Con iteradores
for( auto it = lista_estudiantes.begin(); it != lista_estudiantes.end(); ++it )
  it->listar();
}

// C++14
for( Estudiante estudiante : lista_estudiantes )
  estudiante.listar();
}

Comment received:

  

It's a university assignment. With vectors I have the same program and it works obvious we know that a vector if it has everything necessary for this, now the teacher wants it to be with the stack so I think I will have to look for an alternative.

If you use the stack is a requirement then you will have to choose to empty it every time you want to show its contents ... and re-fill it keeping the order of the elements. And to perform this task we need ... another stack !!!:

stack<Estudiante> temporal;

// Paso 1: listamos elementos
while( !lista_estudiantes.empty() )
{
  Estudiante estudiante = lista_estudiantes.top();
  lista_estudiantes.pop();
  estudiante.listar();

  temporal.push(estudiante);
}

// Paso 2: restauramos la pila
while( !temporal.empty() )
{
  Estudiante estudiante = temporal.top();
  temporal.pop();
  lista_estudiantes.push(estudiante);
}
    
answered by 30.09.2018 / 22:39
source