I’ve been creating a node_add function, that works properly to me,
but I’m having a hard time creating a node_delete one.
Here is what I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
typedef struct friend // The struct
{
char *name;
int age;
char gender;
struct friend* next; // A pointer that points to the next node in the linked list
}friend;
void node_delete(); // The deleting node function
friend* head; // Definging the head of the linked list
void node_delete() // Deleting a node
{
char name[256];
printf ("Please enter the friend's name you want to delete: \n");
fgets (name, 256, stdin);
fgets (name, 256, stdin); // Getting the name of the person that the user wants to delete
while (head -> next != NULL) // As long the node isnt the last one
{
if (0 == (strcmp(head -> name, name))) // If the name that the user entered matchs a name in the linked list,
{ // It'll skip it
head -> next = head -> next -> next; // Deletes a node from the linked list
}
head -> next; // Going to the next node
}
free(head); // Freeing the deleted node
}
I only have a problem in the delete node function
1 Answer