I was going through a book studying Linked list and saw these lines
if( *head == NULL){
}else if ( (*head)->next == (node *) NULL ){
}
what is the difference between NULL
and (node *) NULL
can they be used interchangeably?
typedef struct nodeType{
int info;
struct nodeType *next;
}node;
Whenever you are creating a linked list, we have to initialize it and since it doesn't have any nodes in the start (empty linked list in the beginning), the start/head points to null. That's what is meant by *head=NULL.
The null reference occurs in the link part of the final node of a linked list. A program that maintains a head and a tail reference may set these references to null, which indicates that the list is empty (has no nodes).
current = null merely reassigns a value of this variable. It does not change the underlying Node object.
next != null means when the loop exits, tmp. next will be null , which is only true for the last node.
When comparing pointers, types are not considered, so it is pointless.
The author likely just included it for clarity if it's an introductory book. If it's not an introductory book, then the author either has an odd coding style, or somehow thinks that it's more meaningful.
They can be used interchangeably. But it is non-standard and unusual to typecast NULL
as your code does.
No cast is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With