Simple stack implementation in C:
struct Stack {
char* data;
Stack* prev;
};
void push(char* data, Stack** stack) {
Stack* node = (Stack*)malloc(sizeof(Stack));
node->data = data;
node->prev = *stack;
*stack = node;
}
int main() {
Stack* top = NULL;
push("1", &top);
push("2", &top);
push("3", &top);
}
then top->prev->prev->data
results into 3
same as top->prev->data
and top->data
.
Can anybody explain why?