Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  4. 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));
  1. 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;
     }
    
  2. 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;
    
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  4. 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));
  1. 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;
     }
    
  2. 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;
    
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  4. 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));
  1. 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;
     }
    
  2. 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;
    
Expanded a bit
Source Link
R Sahu
  • 3.6k
  • 13
  • 20
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  4. 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));
  1. 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;
     }
    
  2. 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;
    
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

  4. 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));
  1. 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;
     }
    
  2. 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;
    
Source Link
R Sahu
  • 3.6k
  • 13
  • 20
  1. The name addNode2Head will be better as addNodeToHead. Using the number 2 for the word to is rather poor choice in a function name.

  2. 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;
     }
    
  3. Given the simplified implementation of addNodeToHead, the function addHeadNode might be unnecessary.

lang-c

AltStyle によって変換されたページ (->オリジナル) /