I had a certain query regarding a question I was asked earlier.
I was given this function prototype void StackPop(NodeType *Top) and was told modify the top of the stack without access to the stack object. StackPop function here is a global function and not a member function of the Stack class. After popping, Top should point to the new value of Top element.
As a solution, popping the top of the stack in such a case would only be possible if we pass the address of the top node of the stack to the function while calling. eg: StackPop(&address);
To modify the top of the stack
NodeType* temp;
temp = top;
*top = (*top)->next;
delete temp;
Is this the simple answer to the question or is something else the case that has to be taken care of?
Yes, this is a valid simple answer to the question, with three caveats:
temp = topshould readtemp = *top;*topis NULL (rather than just dereferencing the NULL pointer);void StackPop(Node *Top)] as it would takeNode**as its argument.The only other remark that I have is that
StackPopsounds like it ought to be a member function rather than a free function.