Created
October 27, 2021 06:29
-
-
Save p13i/ee154a681cd108a1d3054ecbbdc8773d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Version 1: tracking if the operations succeed and nesting if-else statements | |
bool LinkedList_Add(LinkedList_t *List, Data_t Data) | |
{ | |
bool Success = true; | |
if (NULL == List) | |
{ | |
Success = false; | |
} | |
else | |
{ | |
LinkedList_Node_t *NewNode = malloc(sizeof(LinkedList_Node_t)); | |
if (NULL == NewNode) | |
{ | |
Success = false; | |
} | |
else | |
{ | |
NewNode->Data = Data; | |
NewNode->Next = List->Head; | |
List->Head = NewNode; | |
} | |
} | |
return Success; | |
} | |
// Version 2: shortcutting when things fail | |
bool LinkedList_Add(LinkedList_t *List, Data_t Data) | |
{ | |
if (NULL == List) | |
{ | |
return false; | |
} | |
LinkedList_Node_t *NewNode = malloc(sizeof(LinkedList_Node_t)); | |
if (NULL == NewNode) | |
{ | |
return false; | |
} | |
NewNode->Data = Data; | |
NewNode->Next = List->Head; | |
List->Head = NewNode; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment