I have a problem adding and removing items from a stack with linked lists. My push function adds but when I want to pop them out with pop it tells me it's empty. Here is the code:
Push functionBOOLEAN lpila_push(LPila p, void* valor){
/*Agregue su codigo de implementacion aqui*/
LPila nuevo, tmp;
CONFIRM_NOTNULL(p,ERROR);
nuevo = (LPila)malloc(sizeof(LPila));
if(nuevo != NULL){
nuevo->valor = valor;
nuevo->sig = NULL;
/*Si la lista esta vacia agrega el elemento al inicio*/
if(p->sig == NULL){
p->sig = nuevo;
printf("Primero agregado\n");
}/*Si ya existen elementos en la lista, recorre y agrega al final*/
else{
tmp = p;
while(tmp->sig != NULL){
tmp = tmp->sig;
}
tmp->sig = nuevo;
printf("Agregado\n");
}
return OK;
}
return ERROR;
}
Pop
BOOLEAN lpila_pop(LPila p, void** valor){
/*Agregue su codigo de implementacion aqui*/
LPila tmp;
CONFIRM_NOTNULL(p,ERROR);
if(p == NULL) printf("lista vacia\n");
tmp = p;
while(tmp != NULL){
tmp = tmp->sig;
}
valor = &tmp->valor;
free(tmp);
return OK;
}
Main
int main(int argc, char** argv) {
LPila p = lpila_crear();
char* data =(char*) malloc(sizeof(char)*10);
if(NULL==p) printf("pila nula! \n");
{
char* test = (char*)malloc(sizeof(char)*10);
char* test1 = (char*)malloc(sizeof(char)*10);
test = "10.000";
test1 = "15.000";
lpila_push(p,&test);
lpila_push(p,&test1);
}
lpila_pop(p,(void**)&data);
printf("data=%s \n",data);
printf("isempty=%d\n", lpila_isEmpty(p) );
lpila_pop(p,(void**)&data);
printf("isempty=%d\n", lpila_isEmpty(p) );
system("pause");
return (EXIT_SUCCESS);
}