C - 从链接列表中弹出最后一项

时间:2015-05-19 18:21:58

标签: c list pointers structure

我正在学习链表,这给我带来了很多麻烦。 我正在通过此调用调用该函数:

pop(&list);

这里是代码:

void pop(NODE** first) {
if(*first != NULL && first!= NULL){
    NODE* ptr = *first;
    while(ptr->next->next != NULL){
        ptr = ptr->next;
    }
    free(ptr->next);
    ptr->next = NULL;   
}

即使我单次调用它也会导致内存泄漏错误。

多次调用此函数时,会出现更多内存泄漏错误。

提前致谢,Mimpopo。

编辑:NODE的定义

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

完整的CODE:

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

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



NODE* insert(NODE *first, int n){
    // create new node
    NODE* new = (NODE*)malloc(sizeof(NODE));
    new->data = n;
    new->next = NULL;

    // if first is NULL, this will be the first
    if(first == NULL)
        return new;

     // otherwise, place it correctly
     NODE* ptr = first;

     // check inserting at the begining
     if(ptr->data > new->data){
        new->next = ptr;
        return new;
     }

     // insert in the middle
     while(ptr->next != NULL){
        if(ptr->next->data > n && ptr->data < n){
            new->next = ptr->next;
            ptr->next = new;
            break;
        }
        ptr = ptr->next;
     }

     // insert at the end of list
     if(ptr->next == NULL){
         ptr->next = new;
     }

    return first;
}

void traverse(NODE *first){
    NODE* ptr = first;
    while(ptr != NULL){
        printf("%d\n", ptr->data);
        ptr = ptr->next;
    }   
}   

NODE* search(NODE *first, int n){
    NODE* ptr = first;
    while(ptr != NULL){
        if(ptr->data == n){
            printf("FOUND %d\n!", n);
            return ptr;
        }
    ptr = ptr->next;
    }
}

int main(){
    NODE* first = NULL;
    NODE* this = NULL;
    first = insert(first, 7);
    first = insert(first, 10);
    first = insert(first, 11);
    first = insert(first, 1);
    first = insert(first, 3);
    first = insert(first, 5);
    first = insert(first, 22);
    first = insert(first, 23);
    first = insert(first, 24);
    first = insert(first, 125);

    pop(&first);




}

1 个答案:

答案 0 :(得分:1)

我没有查看你的所有代码,但是对于函数,它可以用以下方式编写

void pop( NODE ** first ) 
{
    if ( *first != NULL )
    {
        NODE *prev = NULL;
        NODE *current = *first;

        while ( current->next ) 
        {
            prev = current;
            current = current->next;
        }

        if ( prev != NULL ) prev->next = current->next;
        else ( *first = NULL );

        free( current );
    }
}

至于你的函数实现,那么它包含很多错误。例如,在本声明中

if(*first != NULL && first!= NULL){

你应该交换第一次和第二次比较。这就是条件看起来像

if(first != NULL && *first!= NULL){

在本声明中

while(ptr->next->next != NULL){

您必须确保ptr->next不等于NULL。

此外,您不会检查已删除的节点是否是列表的第一个节点。

考虑到功能插入也是错误的。您只考虑此代码段中的一个条件

 while(ptr->next != NULL){
    if(ptr->next->data > n && ptr->data < n){
        new->next = ptr->next;
        ptr->next = new;
        break;
    }
    ptr = ptr->next;
 }

然而,可能是例如

    ptr->next->data >= n && ptr->data < n

    ptr->next->data > n && ptr->data <= n