Hola:
Estoy cometiendo un error en el siguiente codigo, que
genera la siguiente salida:
expected 333 obtainded 1
expected 222 obtainded 1
expected 111 obtainded 1
Necesitaria orientacion con respecto a pop(). Con ddd
ya comprobé que push() funciona. Me imagino que es un
error conceptual de punteros, muy evidente para el que
ya sabe.
Preferiria que quien me conteste, me de mejor una
pista antes que el arreglo.
pd: la versión que envio ha sido reducida a su minima
expresion (le he quitado comprobaciones de error sobre
malloc e isEmpty() por ejemplo) para achicar.
Muchas gracias desde ya
Carlos Pantelides
gcc -ggdb -DDEBUG -fno-inline -std=iso9899:1999 -Wall
-pedantic stack.c
#include <stdio.h>
#include <malloc.h>
typedef struct stacknode {
struct stacknode * next;
void * value;
} stacknode;
typedef struct stacktop {
struct stacknode * next;
} stacktop;
void initStack(stacktop * stack) {
stack->next=NULL;
}
int push(stacktop * stack, void * value){
stacknode *topnode=malloc(sizeof(stacknode));
topnode->value = value;
topnode->next = stack->next;
stack->next = topnode;
return 0;
}
void * pop(stacktop * stack, void * value){
stacknode * top_node;
top_node = stack->next;
value = top_node->value;
stack->next = top_node->next;
free(top_node);
return NULL;
}
int main(){
stacktop realstack;
stacktop * stack = &realstack;
initStack(stack);
int a =111;
int b =222;
int c =333;
int *pa = &a;
int *pb = &b;
int *pc = &c;
void *gp;
int *ip;
push(stack,pa);
push(stack,pb);
push(stack,pc);
pop(stack,gp);
ip = gp;
printf("expected %d obtainded %d\n",c,*ip);
pop(stack,gp);
ip = gp;
printf("expected %d obtainded %d\n",b,*ip);
pop(stack,gp);
ip = gp;
printf("expected %d obtainded %d\n",a,*ip);
}
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
_______________________________________________
Lista de correo Programacion.
[email protected]
http://listas.fi.uba.ar/mailman/listinfo/programacion