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

C 尝试编写删除节点的函数,c,linked-list,C,Linked List,我试图编写一个函数,从链表中删除一个节点,尽管我遇到了麻烦 这是我的算法: 获取要删除的节点的名称 每个节点都有3个详细信息:姓名/年龄/性别 然后我在列表中找到了它的位置 然后我把它传下去 比如说 朋友->下一个=朋友->下一个->下一个 虽然我需要找到链表中的第一个节点,但我不知道如何找到它。 这是我写的: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <std

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

这是我的算法:

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

似乎您应该比较temp->next->name和name,而不是delete->next->name

while (0 == (strcmp(temp -> next -> name, name))) 
但是你应该把你的临时工分配给好友名单的负责人

temp = delete;  // delete should be a pointer to root list element
和。。你为什么需要我和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;
} 
此代码中存在逻辑错误,您还需要检查head节点。在我的代码中没有这个签入。你可以自己轻松添加它。很抱歉已在代码块中修复


此外,delete是函数parametr的坏名称,请尝试friendList或类似的方法。另外,C++中没有删除关键字吗?p> 嗯,你在这方面做得很好

我不完全确定你的问题,但以下是我认为你可能想做的:

#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:%s\n", pstPerson->next->name);
        print_friends(pstPerson->next);
    }
    else 
    {
        printf("no more friends :(\n\n");
    }
}

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("\n\n%s's friends after:\n", stPerson.name);
    print_friends(&stPerson);

    printf("\n\n\ndone!\n");

    return 0;
}

它假设您的node_deletefriend*delete函数接受一个人,在链接列表中有一些朋友,并删除一个与stdin输入匹配的朋友


我还添加了另一个小功能,这样你就可以看到链表是多么有趣

只要名称不匹配,while循环就会停止;您正在覆盖读取到名称中的第一个数据;你从来没有把数据放进temp,只是给它一块内存;i和j未使用。试着仔细阅读并准确解释代码的每一行对您自己的作用,这可能会有所帮助:为什么您要尝试两次获取名称?我已将循环更改为0!=,所以它会起作用;另外,我不知道如何在里面放东西,这是个问题@Redx-我有一个bug,我必须写两次才能让它工作。。这不是问题所在,所以忘了它吧。。。编辑:我会立即添加注释…@AmitSegal,我不是要向我们解释,只是向你自己或一个方便的橡皮鸭或其他对象解释,哈哈,为了让它正常工作,你需要解释每一行上的每个函数调用,比如,为什么有两个fGets?,这是发现bug的最好方法。这有时被称为橡皮鸭调试,它是一个非常有用的技能,甚至有一个!。好的,谢谢你的评论:另外,我用了两个fgets,因为我有一个bug,有时候一个还不够。。。两个FGET就像一个FGET…我已经修复了cmp;我没有,我想我会需要他们,所以我只是把他们在开始。。。关键是,我怎么把东西放在临时的?我知道朋友和删除是一个关键字,虽然我在C,明年我将学习C++,我会把这个记在心里,谢谢!另外,delete应该是指向根列表元素的指针,这是什么意思?我正在用一种不同的语言学习C,所以我不确定你做了什么。我的意思是,当你调用这个函数时,你应该给它一个指向列表头的指针,就像一个参数delete一样。还有一个逻辑错误,我已经在上面的代码块下面描述过了,请不要忘记修复这个错误。这是我从一开始就想做的,但我想不出一个方法来解决这个问题……我已经在代码块中添加了这个修复程序来回答您。希望,我已经设法帮助你了,祝你好运!