Skip to content

Instantly share code, notes, and snippets.

@p13i
Created October 27, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p13i/ee154a681cd108a1d3054ecbbdc8773d to your computer and use it in GitHub Desktop.
Save p13i/ee154a681cd108a1d3054ecbbdc8773d to your computer and use it in GitHub Desktop.
// 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