【问题标题】:The list is not filled in列表未填写
【发布时间】:2020-08-16 11:34:15
【问题描述】:

我编写了一个反转列表的方法,但结果列表仍然为空。帮助我们了解问题所在。

反转列表的方法:

void reverseList(pLIST pL){
    pNODE pN = pL->top;
    pLIST pLReverse = createList();

    while(pN){
        pNODE pNew = malloc(sizeof(NODE));
        pNew->value = pN->value;
        if(!pLReverse->top){
            pNew->next = NULL;
            pLReverse->top = pNew;
        }
        else{
            pNew->next = pLReverse->top;
            pLReverse->top = pNew;              
        }

        pN = pN->next;
    }

    showList(pLReverse);
}

列表的结构:

typedef struct Node{
    int value;
    struct Node * next;
} NODE, *pNODE;

typedef struct List{
    int len;
    pNODE top;
} LIST, *pLIST;

打印列表的方法:

void showList(pLIST pL){
    if(isEmpty(pL)) printf("Empty\n");
    else{
        pNODE temp = pL->top;
        printf("Length: %d\n", pL->len);
        while(temp){
            printf("Pointer: %p\tValue: %d\tNext pointer: %p\n", temp, temp->value, temp->next);
            temp = temp->next;
        }
    }
}

【问题讨论】:

  • 请提供minimal verifiable example。比如createList是如何实现的,reverseList是如何调用的,原始列表是如何构造的?此外,您是否进行了任何调试,以便在调试器中运行程序和/或添加更多调试打印以跟踪程序执行?
  • 你没有增加pLReverse->len
  • 谢谢,你的评论有帮助,我不专心
  • @Макс 不需要将列表创建为指针 pLIST pLReverse = createList();我怀疑返回指向动态分配列表的指针的函数 createList 没有意义..

标签: c linked-list reverse singly-linked-list function-definition


【解决方案1】:

对于初学者来说,为这样的指针引入别名是个坏主意

typedef struct List{
    int len;
    pNODE top;
} LIST, *pLIST;

使用这样的别名,你不能声明一个指向常量列表的指针,因为这个声明

const pLIST list;

不等于

const struct List *list;

它的意思是

struct List * const list;

这不是必需的。

考虑到这个声明

pLIST pLReverse = createList();

您似乎在动态分配列表。没有必要这样做。列表可以声明为具有自动存储期限的对象。

函数reverseList 应该反转传递给它的列表本身,而不是在函数内创建新列表。此外,您还有内存泄漏,因为指针 pLReverse 指向的已创建列表未释放。

这是一个演示程序,展示了如何定义函数 reverseList

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

typedef struct Node
{
    int value;
    struct Node *next;
} Node;

typedef struct List
{
    size_t len;
    Node *head;
} List;

void init( List *list )
{
    list->len = 0;
    list->head = NULL;
}

int pushFront( List *list, int value )
{
    Node *new_node = malloc( sizeof( Node ) );

    int success = new_node != NULL;

    if ( success )
    {
        new_node->value = value;
        new_node->next = list->head;
        list->head = new_node;
        ++list->len;
    }

    return success;
}

void showList( const List *list )
{
    for ( Node *current = list->head; current != NULL; current = current->next )
    {
        printf( "%d -> ", current->value );
    }

    puts( "null" );
}

void reverseList( List *list )
{
    Node *current = list->head;
    list->head = NULL;

    while (  current != NULL )
    {
        Node *new_node = current;
        current = current->next;
        new_node->next = list->head;
        list->head = new_node;
    }
}

void freeList( List *list )
{
    while ( list->head != NULL )
    {
        Node *tmp = list->head;
        list->head = list->head->next;
        free( tmp ); 
    }
}

int main(void) 
{
    List list;
    init( &list );

    const int N = 10;

    for ( int i = 0; i < N; i++ )
    {
        pushFront( &list, i );
    }

    showList( &list );

    reverseList( &list );

    showList( &list );

    freeList( &list );

    return 0;
}

程序输出是

9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 -> null
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多