I want to create a function that adds an element to the end of a linked list. It also has to return 0 if the element was added successfully, or a 1 if memory could not allocated/saved to for the element.
The question is, how do I know if the memory was allocated successfully or if the element was added successfully? This is the code:
int push_back(pos_t *head, int new_value) {
pos_t *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
pos_t *temp1 = (pos_t *)malloc(sizeof(pos_t));
temp1->data = new_value;
temp1->next = NULL;
temp = temp1;
}
temp=temp1;-->temp->next = temp1;?head == NULLpossible? It would be useful to see how the link-list is initialized.