我一直在创建一个 node_add 函数,对我来说可以正常工作,但是我很难创建一个 node_delete 函数。
这是我写的:
#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
}
我只有删除节点功能有问题