I'm creating a singly linked list in C which has head and tail pointers where the head pointer points to starting node of SLL and tail pointer points to the last node of SLL. I don't want to traverse till the end of the list using the head pointer to delete the node. Is there a way so that I can use the tail pointer to delete the last element of SLL?
Following is node addition function. head and tail are initiated NULL.
void add_node_last(Node** head, Node** tail, int data) {
Node* new_node = (Node *) malloc(sizeof(Node));
new_node -> data = data;
new_node -> ptr = NULL;
if(*head == NULL && *tail == NULL) {
*head = new_node;
*tail = new_node;
return;
}
(*tail) -> ptr = new_node;
*tail = new_node;
}
To delete the first node, the following function is used:
void del_first(Node **head) {
if(*head == NULL) {
return;
}
*head = (*head) -> ptr;
free(*head);
}
free(*head);is wrong. - BLUEPIXYNode *temp = *head; *head = (*head) -> ptr; free(temp);Also if become*head == NULLthen Need set tail to NULL. - BLUEPIXY