This is the struct Stack:
Code:
struct Stack {
struct Link {
void* data;
Link* next;
void initialize(void* dat, Link* nxt);
}* head;
void initialize();
void push(void* dat);
void* peek();
void* pop();
void cleanup();
};
Here is the definitions of functions push() and initialize() of struct Stack:
Code:
void Stack::push(void* dat) {
Link* newLink = new Link;
newLink->initialize(dat, head);
head = newLink;
}
void Stack::Link::initialize(void* dat, Link* nxt) {
data = dat;
next = nxt;
}
This is a comment on push():
" (1) The next pointer is assigned to the current head. (2) Then head is assigned to the new Link pointer. (3) This effectively pushes the Link in at the top of the list. "
I don't understand how (1) and (2) cause (3) to happen. Please explain.
Thanks for your interest.