J
Jianwei Sun
Hello, all,
I am reading some code from an interview book.
It has a stack pop function implementation like this..
typedef struct Element {
struct Element *next;
void *data;
} Element;
bool pop( Element **stack, void **data ){
Element *elem;
if (!(elem = *stack)) return false;
*data = elem->data;
*stack = elem->next;
delete elem;
return true;
}
my question is this, since you already delete the element in the pop function,
the data returned will be invalid, since it's part of the element.
Can anybody points out whether I have some misunderstanding here?
Thanks,
J.W.
I am reading some code from an interview book.
It has a stack pop function implementation like this..
typedef struct Element {
struct Element *next;
void *data;
} Element;
bool pop( Element **stack, void **data ){
Element *elem;
if (!(elem = *stack)) return false;
*data = elem->data;
*stack = elem->next;
delete elem;
return true;
}
my question is this, since you already delete the element in the pop function,
the data returned will be invalid, since it's part of the element.
Can anybody points out whether I have some misunderstanding here?
Thanks,
J.W.