0

我真的不明白我的程序会发生什么,因为当我尝试删除链接列表中的任何记录时,它似乎工作正常。

当我尝试删除2它并没有删除它并打印的记录时,问题就出现了0

这是可以验证的工作程序:

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node *next;
};

void printList( struct node  *head );
void freeList ( struct node **head );
void createList ( struct node **head, const int val );
int searchInList( struct node **head);
int deleteInList( struct node **head, const int val );

int main ( void ){
    struct node *head = NULL;

    int listLen = 5;
    int remove = 3;

    createList ( &head , listLen );
    printf("\t\tBefore:\n");
    printList( head );

    if ( deleteInList( &head, remove ) == -1 ){
        printf("No result found!\n\t\tNothing here to be deleted\n");
    }else{
        printf("\t\tAfter:\n");
        printList( head );
    }

    freeList( &head );
}

void printList( struct node *head ){
    struct node *current = head;

    while ( current != NULL ){
        printf("Data = %d\n",  current->data );
        current = current->next;
    }
    printf("\n\n");
}

void freeList ( struct node **head ){
    struct node *current = *head;
    while ( current != NULL ){
        struct node *tmp = current->next;
        free( current );
        current = tmp;
    }
}

void createList ( struct node **head, const int val ){
    struct node *current;
    for( int i = val ; i > 0 ; i-- ) {
        current = malloc( sizeof( struct node ) );
        current->data = i;
        current->next  = *head;
        *head = current;
    }
}

int searchInList( struct node ** head) {
    int retval = -1;
    struct node *next = NULL;

    if (*head == NULL) {
        return -1;
    }

    next = (*head)->next;
    retval = (*head)->data;
    free(*head);
    *head = next;

    return retval;
}

int deleteInList( struct node ** head, const int val ) {
    struct node *previous, *current;

    if (*head == NULL) {
        return -1;
    }

    if ( ( *head )->data == val ) {
        return searchInList( head );
    }

    current = ( *head )->next;
    previous = current;

    while ( current ) {
        if ( current->data == val ) {
            previous->next = current->next;
            free( current );
            return val;
        }

        previous = current;
        current  = current->next;
    }
    return -1;
}

输出:

        Before:
Data = 1
Data = 2
Data = 3
Data = 4
Data = 5


        After:
Data = 1
Data = 2
Data = 4
Data = 5

该程序看起来不错,但如果我用它替换int remove = 3;int remove = 2;不起作用,因为我得到:

Data = 1
Data = 2
Data = 3
Data = 4
Data = 5


        After:
Data = 1
Data = 0
Data = 3
Data = 4
Data = 5

当预期的输出应该是:

        After:
Data = 1
Data = 3
Data = 4
Data = 5

看着Valgrind我注意到我还有一个free

==9107== HEAP SUMMARY:
==9107==     in use at exit: 0 bytes in 0 blocks
==9107==   total heap usage: 6 allocs, 7 frees, 1,104 bytes allocate

而且我无法弄清楚发生了什么。

这是整个Valgrind报告:

==9107== Memcheck, a memory error detector
==9107== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9107== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9107== Command: ./program
==9107== 
        Before:
Data = 1
Data = 2
Data = 3
Data = 4
Data = 5


        After:
Data = 1
==9107== Invalid read of size 4
==9107==    at 0x400714: printList (program.c:39)
==9107==    by 0x4006D2: main (program.c:29)
==9107==  Address 0x5204130 is 0 bytes inside a block of size 16 free'd
==9107==    at 0x4C2EDEB: free (vg_replace_malloc.c:530)
==9107==    by 0x4008C7: deleteInList (program.c:97)
==9107==    by 0x4006AB: main (program.c:25)
==9107==  Block was alloc'd at
==9107==    at 0x4C2DB8F: malloc (vg_replace_malloc.c:299)
==9107==    by 0x4007AA: createList (program.c:57)
==9107==    by 0x400684: main (program.c:21)
==9107== 
Data = 2
==9107== Invalid read of size 8
==9107==    at 0x40072B: printList (program.c:40)
==9107==    by 0x4006D2: main (program.c:29)
==9107==  Address 0x5204138 is 8 bytes inside a block of size 16 free'd
==9107==    at 0x4C2EDEB: free (vg_replace_malloc.c:530)
==9107==    by 0x4008C7: deleteInList (program.c:97)
==9107==    by 0x4006AB: main (program.c:25)
==9107==  Block was alloc'd at
==9107==    at 0x4C2DB8F: malloc (vg_replace_malloc.c:299)
==9107==    by 0x4007AA: createList (program.c:57)
==9107==    by 0x400684: main (program.c:21)
==9107== 
Data = 3
Data = 4
Data = 5


==9107== Invalid read of size 8
==9107==    at 0x400764: freeList (program.c:48)
==9107==    by 0x4006DE: main (program.c:32)
==9107==  Address 0x5204138 is 8 bytes inside a block of size 16 free'd
==9107==    at 0x4C2EDEB: free (vg_replace_malloc.c:530)
==9107==    by 0x4008C7: deleteInList (program.c:97)
==9107==    by 0x4006AB: main (program.c:25)
==9107==  Block was alloc'd at
==9107==    at 0x4C2DB8F: malloc (vg_replace_malloc.c:299)
==9107==    by 0x4007AA: createList (program.c:57)
==9107==    by 0x400684: main (program.c:21)
==9107== 
==9107== Invalid free() / delete / delete[] / realloc()
==9107==    at 0x4C2EDEB: free (vg_replace_malloc.c:530)
==9107==    by 0x400777: freeList (program.c:49)
==9107==    by 0x4006DE: main (program.c:32)
==9107==  Address 0x5204130 is 0 bytes inside a block of size 16 free'd
==9107==    at 0x4C2EDEB: free (vg_replace_malloc.c:530)
==9107==    by 0x4008C7: deleteInList (program.c:97)
==9107==    by 0x4006AB: main (program.c:25)
==9107==  Block was alloc'd at
==9107==    at 0x4C2DB8F: malloc (vg_replace_malloc.c:299)
==9107==    by 0x4007AA: createList (program.c:57)
==9107==    by 0x400684: main (program.c:21)
==9107== 
==9107== 
==9107== HEAP SUMMARY:
==9107==     in use at exit: 0 bytes in 0 blocks
==9107==   total heap usage: 6 allocs, 7 frees, 1,104 bytes allocated
==9107== 
==9107== All heap blocks were freed -- no leaks are possible
==9107== 
==9107== For counts of detected and suppressed errors, rerun with: -v
==9107== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

我在 Linux mint 18.3 上GCC-7

4

2 回答 2

1

deleteInList

  ...
  if ((*head)->data == val) {
    return searchInList(head);
  }

  current = (*head)->next;
  //  previous = current;   << this is wrong
  previous = *head;         // this is correct

  while (current) {
    if (current->data == val) {
  ...

在您的原始代码中与第一次迭代期间previous相同。current

顺便说一句:该searchInList功能并不像它的名字所暗示的那样。

于 2018-02-21T11:20:52.057 回答
0

[风格]

  • 您使用了太多变量,所有变量都需要保持同步。
  • 您正在为*head节点创建特殊情况,从而导致多个代码路径。

避免特殊情况会将代码减少到一个循环,一个额外的条件和一个额外的变量:


int deleteInList( struct node ** head, const int val ) 
{
struct node *current;

for( ; (current = *head); head = &current->next) {
        if (current->data != val) continue;
        *head = current->next;
        free(current);
        return val;
        }

return -1;
}
于 2018-02-21T11:48:13.213 回答