c - 尝试编写一个删除节点的函数

标签 c linked-list

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

这是我的算法:

  • 获取我要删除的节点的名称 (每个节点都有3个详细信息:姓名/年龄/性别)
  • 然后我在列表中找到它的位置
  • 然后我将其转发

例如

Friend -> next = friend -> next -> next..

虽然我需要找到链表中的第一个节点,但我不知道如何找到它。 这是我写的:

#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:%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;

}

online demo.

它假设您的 node_delete(friend* delete) 函数接受一个人,在链接列表中包含一些 friend ,并删除一个姓名与标准输入匹配的 friend 。

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

关于c - 尝试编写一个删除节点的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10350619/

相关文章:

c - 是否存在 strcmp() 返回 -1 0 和 1 以外的值的编译器和库?

c - C : Interface implemented via multiple C files 中的自上而下方法

c - 变量在声明之前使用是怎么回事?

c - 如何对正在增长的链表中的 2 个节点进行所有可能的比较

c - 这两种设置指针相等的方法是否相同?

c - 字符串矩阵中的字符串分配(不是在声明时)

c++ - LinkedList/Stack/Queue - 帮助出队

c++ - 如何跟踪单链表的开头?

c - 链表不打印所有值

c++ - 复制构造函数和赋值运算符问题