C语言 尝试编写删除节点的函数



我试图编写一个从链表中删除节点的函数,尽管我遇到了麻烦。

这是我的算法:

  • 获取要删除的节点的名称(每个节点都有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);
}

好吧,你在这方面做得很好。

我不完全确定你的问题,但这是我认为你可能试图做的事情:

#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(const char* name, friend* stFriend)
{
    if (!stFriend->next) //end of list
    { 
        printf("%s is not a friend!n", name);
    }
    else if ( !strcmp(name, stFriend->next->name) ) //name matches! remove link
    {     
        //here's where you can free your unwanted friend ptr ~NB: are you sure this friend is not a friend of someone else?
        //free(stFriend -> next);    
        stFriend->next=stFriend->next->next;
        printf("%s is no longer a friend!n", name); 
    }
    else //name does not match -recurse
    {
        node_delete(name, stFriend->next);
    }
}

void print_friends(const friend* pstPerson)
{
    if (pstPerson->next)
    {
        printf("next friend:%sn", pstPerson->next->name);
        print_friends(pstPerson->next);
    }
    else 
    {
        printf("no more friends :(nn");
    }
}
int main()
{
    friend stFriend0, stFriend1, stFriend2, stPerson;
    char name[256]={0};
    stFriend0.name="amber";
    stFriend0.next=0;
    stFriend1.name="betty";
    stFriend1.next=&stFriend0;
    stFriend2.name="catherine";
    stFriend2.next=&stFriend1;
    stPerson.name="violet";
    stPerson.next=&stFriend2;
    printf("%s's friends before:n", stPerson.name);
    print_friends(&stPerson);
    printf("remove a friend: ");
    fgets (name, 256, stdin);
    strtok(name, "n");
    node_delete(name, &stPerson);
    printf("nn%s's friends after:n", stPerson.name);
    print_friends(&stPerson);
    printf("nnndone!n");
    return 0;

}

在线演示。

它假设您的node_delete(friend* delete)函数将一个人与链表中的一些朋友一起删除,并删除姓名与 stdin 输入匹配的朋友。

(我还添加了另一个小函数,这样您就可以看到链表可以有多有趣!

似乎

您应该将(temp -> 旁边的 -> 名称)与 (名称) 进行比较,而不是与(删除 -> 旁边的 -> 名称)

while (0 == (strcmp(temp -> next -> name, name))) 

但是您应该将临时工与朋友列表负责人一起分配

temp = delete;  // delete should be a pointer to root list element

和。。出于什么原因你需要 i 和 j?还有为什么你使用 malloc()?为什么不将您的结构声明为本地内容

friend* temp;

试试这个

friend* temp; 
char name[256];
// receive node to delete
printf ("Please enter the friend's name you want to delete: n");
// omg here was two fgets!!
fgets (name, 256, stdin);
// delete should be a pointer to root list element
temp = delete;    
// Check if node to be deleted is a root node
if(strcmp(temp -> name, name)==0){ 
    delete = delete->next;
    return;
}   
// go throug all the list
while (temp->next!=NULL){
   // if node was found - delete it
   if(strcmp(temp -> next -> name, name)==0) 
       temp -> next = temp -> next -> next;    
   temp = temp-> next;
} 

此代码中存在逻辑错误,您还需要检查头节点。我的代码中没有这种检查。您可以自己轻松添加。不好意思。(已在代码块中修复)

此外,"删除"是函数参数的坏名称,请尝试朋友列表或类似的东西更好)。另外,删除关键字不是C++吗?

相关内容

  • 没有找到相关文章

最新更新