Frequently Asked Questions for interviews: reverse operations for Single-Chain tables/reverse links for linked lists click to open
Void init_node (node * tail, char * init_array)
In this way, the declared function is incorrect. The function is originally intended to initialize the linked list through arrays. If the linked list node passes in a pointer, the linked list cannot be created unless it is a two-dimensional pointer, that is, a pointer to the pointer, or a reference pointing to a pointer.
Although the input is a pointer, the operation on the form parameter does not affect the real parameter, and the copy of the real parameter is modified in the function. To modify input parameters within a function, either pass in the reference of the real parameter or the address of the real parameter.
Pointer Reference
Void init_node_by_referenceToPointer (node * & tail, const char * init_array)
{
Node * tmp = NULL;
Int j = strlen (init_array );
For (int I = 0; I <j; I ++)
{
Tmp = new node;
Tmp-> data = * (init_array + I );
Tmp-> next = tail;
Tail = tmp;
}
}
/***************************************
Such a statement is wrong !!!
Void init_node_by_referenceToPointer (node & * tail, char * init_array)
Error: cannot declare pointer to 'class node &'
****************************************/
Pointer to pointer
Void init_node_by_pointerToPointer (node ** tail, const char * init_array)
{
Node * tmp = NULL;
Int j = strlen (init_array );
For (int I = 0; I <j; I ++)
{
Tmp = new node;
Tmp-> data = * (init_array + I );
Tmp-> next = * tail;
* Tail = tmp;
}
}