-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteNode.c
More file actions
23 lines (22 loc) · 970 Bytes
/
deleteNode.c
File metadata and controls
23 lines (22 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**Function to remove a node from the list and alter the pointers accordingly to not disrupt the order of the
*data structure.
*@pre List must exist and have memory allocated to it
*@post toBeDeleted will have its memory freed if it exists in the list.
*@param list pointer to the dummy head of the list containing deleteFunction function pointer
*@param toBeDeleted pointer to data that is to be removed from the list
*@return returns EXIT_SUCCESS on success, and EXIT_FAILURE when empty. Returns -1 when the node cannot be found.
**/
int deleteNodeFromList(List *list, void *toBeDeleted){
Node *nodePos = NULL;
nodePos = list->head;
while(nodePos != NULL && list->compare(toBeDeleted, nodePos->data)!=0){
nodePos = nodePos->next;
}
Node *front, *back;
front = nodePos->next;
front->previous = nodePos->previous;
back = nodePos->previous;
back->next = nodePos->next;
free(nodePos);
return EXIT_SUCCESS;
}