The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.Don't explicitly cast the return value of
malloc
. See Do I cast the result of malloc? Do I cast the result of malloc?
Instead of
sList *psList =(sList *) malloc(sizeof(sList));
use
sList *psList = malloc(sizeof(sList));
Always check the return value of
malloc
before proceeding to use it.sList *newNode(void) { sList *psList = malloc(sizeof(sList)); if ( psList == NULL ) { return NULL; } psList->nextNode = NULL; return psList; }
Make sure that you check the value returned by the above function.
sList* node = newNode(); if ( node == NULL ) { // Decide how you want to handle the error. // return NULL??? } node->data = data;
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.Don't explicitly cast the return value of
malloc
. See Do I cast the result of malloc?
Instead of
sList *psList =(sList *) malloc(sizeof(sList));
use
sList *psList = malloc(sizeof(sList));
Always check the return value of
malloc
before proceeding to use it.sList *newNode(void) { sList *psList = malloc(sizeof(sList)); if ( psList == NULL ) { return NULL; } psList->nextNode = NULL; return psList; }
Make sure that you check the value returned by the above function.
sList* node = newNode(); if ( node == NULL ) { // Decide how you want to handle the error. // return NULL??? } node->data = data;
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.Don't explicitly cast the return value of
malloc
. See Do I cast the result of malloc?
Instead of
sList *psList =(sList *) malloc(sizeof(sList));
use
sList *psList = malloc(sizeof(sList));
Always check the return value of
malloc
before proceeding to use it.sList *newNode(void) { sList *psList = malloc(sizeof(sList)); if ( psList == NULL ) { return NULL; } psList->nextNode = NULL; return psList; }
Make sure that you check the value returned by the above function.
sList* node = newNode(); if ( node == NULL ) { // Decide how you want to handle the error. // return NULL??? } node->data = data;
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.Don't explicitly cast the return value of
malloc
. See Do I cast the result of malloc?
Instead of
sList *psList =(sList *) malloc(sizeof(sList));
use
sList *psList = malloc(sizeof(sList));
Always check the return value of
malloc
before proceeding to use it.sList *newNode(void) { sList *psList = malloc(sizeof(sList)); if ( psList == NULL ) { return NULL; } psList->nextNode = NULL; return psList; }
Make sure that you check the value returned by the above function.
sList* node = newNode(); if ( node == NULL ) { // Decide how you want to handle the error. // return NULL??? } node->data = data;
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.Don't explicitly cast the return value of
malloc
. See Do I cast the result of malloc?
Instead of
sList *psList =(sList *) malloc(sizeof(sList));
use
sList *psList = malloc(sizeof(sList));
Always check the return value of
malloc
before proceeding to use it.sList *newNode(void) { sList *psList = malloc(sizeof(sList)); if ( psList == NULL ) { return NULL; } psList->nextNode = NULL; return psList; }
Make sure that you check the value returned by the above function.
sList* node = newNode(); if ( node == NULL ) { // Decide how you want to handle the error. // return NULL??? } node->data = data;
The name
addNode2Head
will be better asaddNodeToHead
. Using the number2
for the wordto
is rather poor choice in a function name.The function
addNodeToHead
can be simplified to:sList *addNodeToHead(sList *psList, void *data) { sList* node = newNode(); node->data = data; // It doesn't matter what psList is. The new // node is now the head of the list. node->nextNode = psList; return node; }
Given the simplified implementation of
addNodeToHead
, the functionaddHeadNode
might be unnecessary.