I have something very simple in C ++ (trying to remember it) a library that I call element.h:
using namespace std;
typedef struct elemento{
int clave;
string nombre;
string apellido;
struct elemento *prox;
}Elemento;
Elemento crearElemento(int clave, string nombre, string apellido){
Elemento e;
e.clave = clave;
e.nombre = nombre;
e.apellido = apellido;
e.prox = NULL;
return e;
}
void imprimirElemento(Elemento e){
cout << "-----------------------------------------------" << endl;
cout << "CLAVE =" << e.clave << endl;
cout << "NOMBRE =" << e.nombre << endl;
cout << "APELLIDO =" << e.apellido << endl;
cout << "-----------------------------------------------" << endl;
}
And I have another library PilaElemento.h:
using namespace std;
typedef Elemento *Pila;
Pila crearPila(){
Pila aux = NULL;
return aux;
}
void apilar(Pila &pila, Elemento e) {
Pila q = new(struct elemento);
e.prox = pila;
q = &e;
pila = q;
}
void desapilar(Pila &pila) {
Pila aux;
aux = pila;
pila = pila->prox;
delete aux;
}
Elemento tope(Pila &pila){
return (elemento)*pila;
}
bool esVacia(Pila &pila) {
bool log = false;
if (pila==NULL) log= true;
return log;
}
Everything works fine, except Unstack, it shows me the following error message:
stack (5291,0x7fff765f5000) malloc: error for object 0x7fff5f143220: pointer being freed was not allocated * set to breakpoint in malloc_error_break to debug Abort trap: 6
Basically I wanted to leave it this way so as not to touch the Pile Library, but only the element library (and I do not want to use POO)