0

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

这是我的算法:

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

2 回答 2

0

似乎您应该将 (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

并且.. 你需要 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;
} 

这段代码存在逻辑错误,还需要检查头节点。我的代码中没有这个检查。您可以轻松地自己添加它。对不起。(已经在代码块中修复)

“删除”也是函数参数的坏名,试试friendList或类似的更好的东西)。另外不是删除 C++ 中的关键字吗?

于 2012-04-27T12:38:41.810 回答
0

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

我不完全确定您的问题,但这是我认为您可能正在尝试做的事情:

#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_delete(friend* delete)函数需要一个人,在链表中有一些朋友,并删除一个名字与标准输入匹配的朋友。

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

于 2012-04-27T16:14:26.350 回答