Two methods to initialize a linked list through Arrays: pointing to the pointer references node * & amp; tail and pointing to the pointer (two-dimensional pointer) node

Source: Internet
Author: User

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;
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.