Deleting nodes from a linked list
Hello! I'm struggling a bit with the function of deleting a node from a linked list. I've made an attempt, and it works fine if the node being deleted is the first node. However, if I want to delete any other node, the function hangs and just starts printing off whatever I input into they keyboard. I've included my node definition and the two functions I was trying to implement.
Thanks in advance!
Code:
typedef struct node
{
char last[1024];
char first[1024];
char position;
int value;
struct node *link;
}Node;
Code:
Node* deleteNode(Node *head)
{
char lastName[MAX_LENGTH+1];
safegets(lastName,MAX_LENGTH+1);
bool found = false;
if (head == NULL)
return NULL;
if (strcmp((head->last),lastName)==0)
{
Node *temp = head->link;
free(head);
return temp;
}
Node *current = head;
while (!found && (current->link)!=NULL)
{
if (strcmp((current->link->last),lastName)==0)
found==true;
else
current=current->link;
}
if (current->link!=NULL)
{
Node *temp = current->link;
(current->link)=(current->link->link);
free(temp);
return head;
}
else if (current->link==NULL)
{
if(strcmp((current->last),lastName)==0)
deleteLast(head);
}
return head;
}
void deleteLast(Node *head)
{
Node *current = head;
while (current->link->link != NULL)
current=current->link;
free(current->link);
current->link=NULL;
}