I'm using C++ to learn consolidated lists in class.
I'm writing a program that displays the list, but I can't tell the difference between current!=NULL;
and current->next!=NULL
.current is the pointer to move through the list.
I may not be able to explain enough, but if anyone knows, please let me know.
void display_every_item(node*head)
{
node*current=head;
while(current!=NULL)//while(cu)
{
cout<<current->data<';';
current=current->next;
}
}
// Display JUST the last item
void display_just_last(node*head)
{
if(NULL==head) // if(!head) - nothing to display
return;
node*current=head;
while (current->next!=NULL) // Stop at the last node
{
current=current->next;
}
cout<<"The last item is:"<current->data<endl;
As for the difference between the two, the answer is that different descriptions are used because of the difference in the results of the functions display_every_item
and display_just_last
to which they belong.
The name of the function itself is helpful for your explanation, so please include that information.
current!=NULL
determines whether the pointer to the current item is a valid value for display_every_item
(displaying all items (=nodes) each time).If this is a valid item, view the content data and move to the next item.current->next!=NULL
determines whether the pointer to the following items is valid for display_just_last
(only the last item (=node)):If the pointer to the next item is NULL, we found the last item, so we will exit the loop.However, the former display_every_item
is redundant and the decision location and way of thinking changes, but you can also use current->next!=NULL
instead of current!=NULL
.
Please refer to this article.
current!=NULL
:current->next!=NULL
: (although C is not C++)
If NULL is added to the current, I will not know where I was on the list, so
I think it is determining whether current->next is NULL.
If current->next is NULL, current is the end of the list.
© 2024 OneMinuteCode. All rights reserved.