問題がありましたが、リンクされたリストからノードを削除する関数を作成しようとしていました。
これが私のアルゴリズムです:
- 削除したいノードの名前を取得します (すべてのノードには、名前/年齢/性別の 3 つの詳細があります)
- 次に、リスト内の場所を見つけます
- そして、私はそれを前に渡します
例えば
友達 -> 次 = 友達 -> 次 -> 次..
リンクされたリストの最初のノードを見つける必要がありますが、そこに到達する方法がわかりません。これは私が書いたものです:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
typedef struct friend
{
char *name;
int age;
char gender;
struct friend* next;
}friend;
void node_delete(friend* delete)
{
friend* temp = malloc(sizeof(friend));
char name[256];
int i = 0, j =0; // Not being used, though I'd use it
printf ("Please enter the friend's name you want to delete: \n");
fgets (name, 256, stdin); // Getting the name of the person the user wants to delete
fgets (name, 256, stdin);
while (0 == (strcmp(temp -> next -> name, delete -> next -> name))) // As long as the
// name doesnt match, it'll go to the next name in the linked list
{
temp = friend -> next; // Going to the next name in the linked list
}
temp -> next = temp -> next -> next; // Replacing the node with the node after it..
// for ex. if I have 1 -> 2 -> 3, it'll be 1 -> 3
free (delete);
}