cLanguage Parameters

Asked 2 years ago, Updated 2 years ago, 124 views

When searching in a linked list, you search by creating a ListNode *p variable as shown below.

void print_list(ListNode *head)
{
    for(ListNode *p = head ; p != NULL ; p = p->link)
        printf("%d->",p->data);
 }

Recurring tree navigation does not create a separate TreeNode*t, just uses the parameter node as it is

TreeNode* searh(TreeNode* node, int key) 
{ 
  while (node != NULL) { 
    if (key == node->key) 
    return node; 
    else if (key < node->key) 
    node = node->left; 
    else 
    node = node->right; 
 } 
 return NULL; 
} 

The book says it's okay because it's a copy of the original variable I don't know why I made ListNode*p for linked list Isn't the linked list also not affected by the original?

tree call-by-reference pointer

2022-09-20 19:36

3 Answers

It sounds like a question of why there is a local pointer variable unnecessarily.

[Voiceover] Right. It doesn't seem necessary.

In terms of readability, if you had the variable name head and the whole list head factor, you could reduce confusion when you use the function, so you might have named it head, but it's not good to write it as a variable that represents a node that keeps changing, so I think we've prepared a local variable.


2022-09-20 19:36

A list is a set of associations of nodes, where the node structure variable and the list structure variable must exist, respectively.

However, in the case of a tree, the tree itself is a recursive data structure. So the tree node itself is also a tree.

Therefore, the list sometimes controls the node variable and the list variable. Because the tree variable is the tree itself, you only need to control the tree variable.

This is probably emphasized in the book you're reading.

In the code you asked, in the case of the list code, the list is a connection of nodes, so if you put in the address value of the first node of the list, you can find it from the first node and print the value.

The code in the tree is to find a node with a key value by putting the address value of the root node of the tree as a parameter and continuing to go down, since the subtree address of the tree is already stored in the left and right variables if the tree is already configured.


2022-09-20 19:36

I made a big mistake. Sorry for the confusion.

As Daewon said, it does not change from head = head->link.

If you take the address value of the list and use the internal head variable (although you won't normally use it like this), it can change.

void print_list(List *plist)
{
    for(; plist->head != NULL; plist->head=plist->head->link)
        ...
}
void print_list(ListNode *head)
{
    for(ListNode *p = head ; p != NULL ; p = p->link)
        printf("%d->",p->data);
 }

This is a common code that first copies the address value stored in the head to p and then changes the value of p. So the head value doesn't change.

If you don't copy and write as above, but just use the head as you asked, the address value handed over when calling the function will change. Change from head = head->link.

void print_list(ListNode *head)
{
    for(; head != NULL ; head = head->link)
        printf("%d->",head->data);
 }

Because head usually refers to the head of the list, you should not copy and modify the head pointer variable like a book when you print the value.

Change the head only if you want to insert a new value into the head of the list or clear the head node of the list.


2022-09-20 19:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.