函数表示节点被删除,但未被删除。

时间:2022-12-25 21:43:04

So I have this function deleteNode which deletes a node given the firstName of a person in struct PersonalInfo in a singly linked list. It returns 1, meaning it deleted the node, but when I print the linked list it leaves the name in the memory as if nothing happened. If anyone could help me out with this, it would be greatly appreciated.

因此,我有这个函数deleteNode,它删除了一个在struct PersonalInfo中一个person的firstName,在一个单独的链表中。它返回1,这意味着它删除了节点,但是当我打印链接列表时,它会在内存中留下名字,就好像什么都没有发生一样。如果有人能帮我解决这个问题,我将不胜感激。

int deleteNode(PersonalInfo **head, char *firstName){
  if(head!=NULL){
    PersonalInfo *currNode = *head;
    while(currNode!=NULL){
      if(strcmp(currNode->next->firstName, firstName)){ 
        PersonalInfo *nextNode = currNode->next->next;
        free(currNode->next);
        currNode->next = nextNode;
        return 1;
      }   
      if(currNode->next==NULL){
        printf("Operation was unsuccessful: No such name exists\n");
        return 0;
      }   
      currNode = currNode->next;
    }   
  }
  else{
    printf("Head is null, please enter a valid head\n");
    return 0;
  }
}

3 个解决方案

#1


2  

When the two strings are equal strcmp returns 0. This is the test you should use.

当两个字符串相等时,strcmp返回0。这是您应该使用的测试。

if(strcmp(currNode->next->firstName, firstName) == 0) 

#2


1  

another possiblity is to replace :

另一种可能性是:

if(strcmp(currNode->next->firstName, firstName))

with :

:

if(!strcmp(currNode->next->firstName, firstName))

Since the return value of str cmp will represent "Flase" only if the strings are the same.

因为str cmp的返回值只在字符串相同的情况下表示“Flase”。

#3


0  

First observation is your strcmp. That block statement will run only for when the strings are unequal. You're not checking for equality. I am assuming you want to run the block statement only when the strings are equal.

第一个观察是你的strcmp。该语句只在字符串不相等时运行。你不是在检查平等。我假设您只需要在字符串相等的情况下运行block语句。

The return values are

返回值是

  1. > 0: str1 > str2
  2. > 0:str1 > str2。
  3. < 0: str1 < str2
  4. < 0:str1 < str2。
  5. = 0: str1 == str2

    = 0:str1 == str2。

    if(strcmp(currNode->next->firstName, firstName) == 0)

    如果(strcmp(currNode - >下一步- > firstName,firstName)= = 0)

#1


2  

When the two strings are equal strcmp returns 0. This is the test you should use.

当两个字符串相等时,strcmp返回0。这是您应该使用的测试。

if(strcmp(currNode->next->firstName, firstName) == 0) 

#2


1  

another possiblity is to replace :

另一种可能性是:

if(strcmp(currNode->next->firstName, firstName))

with :

:

if(!strcmp(currNode->next->firstName, firstName))

Since the return value of str cmp will represent "Flase" only if the strings are the same.

因为str cmp的返回值只在字符串相同的情况下表示“Flase”。

#3


0  

First observation is your strcmp. That block statement will run only for when the strings are unequal. You're not checking for equality. I am assuming you want to run the block statement only when the strings are equal.

第一个观察是你的strcmp。该语句只在字符串不相等时运行。你不是在检查平等。我假设您只需要在字符串相等的情况下运行block语句。

The return values are

返回值是

  1. > 0: str1 > str2
  2. > 0:str1 > str2。
  3. < 0: str1 < str2
  4. < 0:str1 < str2。
  5. = 0: str1 == str2

    = 0:str1 == str2。

    if(strcmp(currNode->next->firstName, firstName) == 0)

    如果(strcmp(currNode - >下一步- > firstName,firstName)= = 0)