【问题标题】:Linked list prints extra 0 at the beginning链表在开头打印额外的 0
【发布时间】:2016-10-13 22:52:30
【问题描述】:

我有一个非常基本的单链表实现。然而,我的实现的问题是它在列表的开头打印了一个额外的零,而我没有明确地为这个额外的节点分配任何存储空间。相同的代码如下 -

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

#define LEN 7

/* List node data structure */
typedef struct _ll_node_ {
    int data;
    struct _ll_node_ *next;
} node;

/*
 * @brief   Utility to print the state of the list
 */
void print_list(node *head)
{
    int i = 0;
    node *tmp = head;
    while (tmp)
    {
        printf("Node:\t%d,\tValue:\t%d\n", ++i, tmp->data);
        tmp = tmp->next;
    }
    printf("\n");
}

/*
 * @brief   Utility to add nodes to the list
 */
node *add_node(node *head, int data)
{
    node *tmp;
    if (head == NULL)
    {
        head = malloc(sizeof(node));
        assert(head != NULL);
        head->data = data;
        head->next = NULL;
    }
    else
    {
        tmp = head;
        while (tmp->next)
            tmp = tmp->next;
        tmp->next = malloc(sizeof(node));
        assert(tmp->next != NULL);
        tmp = tmp->next;
        tmp->data = data;
        tmp->next = NULL;
    }
    return head;
}

/*
 * @brief   Driver function
 */
int main(int argc, char *argv[])
{
    node *head = NULL;
    int i = 0;
    /* Allocate memory */
    head = malloc(LEN * sizeof(node));
    assert(head != NULL);
    /* Populate the list */
    for (; i < LEN; i++)
        head = add_node(head, rand() % 1000);
    /* Print its state */
    print_list(head);

    return 0;
}

有人可以帮我找出我做错了什么吗?

System information:
    Distributor ID: Ubuntu
    Description:    Ubuntu 14.04.3 LTS
    Release:        14.04
    Codename:       trusty

【问题讨论】:

  • 评论这两行head = malloc(LEN * sizeof(node)); assert(head != NULL);,其余一切都很好。

标签: c pointers linked-list malloc singly-linked-list


【解决方案1】:

此声明

head = malloc(LEN * sizeof(node));

没有意义。删除它。

您分配了未初始化的数组。所以使用函数 add_node 会导致未定义的行为。

考虑到函数add_node 如果通过引用传递头部,则可以更简单地编写。例如

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

#define LEN 7

/* List node data structure */
typedef struct _ll_node_ {
    int data;
    struct _ll_node_ *next;
} node;

/*
 * @brief   Utility to print the state of the list
 */
void print_list(node *head)
{
    int i = 0;
    node *tmp = head;
    while (tmp)
    {
        printf("Node:\t%d,\tValue:\t%d\n", ++i, tmp->data);
        tmp = tmp->next;
    }
    printf("\n");
}

/*
 * @brief   Utility to add nodes to the list
 */
int add_node( node **head, int data )
{
    int success;

    while ( *head != NULL ) head = &( *head )->next;

    *head = malloc( sizeof( node ) );

    success = *head != NULL;

    if (success )
    {
        ( *head )->data = data;
        ( *head )->next = NULL;
    }

    return success;
}

/*
 * @brief   Driver function
 */
int main( void )
{
    node *head = NULL;
    int i = 0;

    srand( ( unsigned int )time( NULL ) );

    /* Populate the list */
    for ( ; i < LEN; i++ )
        add_node( &head, rand() % 1000);
    /* Print its state */
    print_list( head );

    return 0;
}

【讨论】:

    【解决方案2】:

    您已经在 main 中为 head 分配内存,因此第一个节点永远不会分配数据,因此默认情况下它为 0。 试试这个:

    int main(int argc, char *argv[])
    {
        node *head = NULL;
        int i = 0;
    
        /* Populate the list */
        for (; i < LEN; i++)
            head = add_node(head, rand() % 1000);
        /* Print its state */
        print_list(head);
    
        return 0;
    }
    

    【讨论】:

    • 感谢@vlad-from-moscow 和 priyansh-goel 指出。完全错过了。
    • @rurtle : 更好的感谢方式是接受答案。
    • @rurtle :我并不急于在 SO 上加分。任何对你有用的答案都应该被接受。我不介意您是否接受弗拉德的回答。 :) 只是如果它解决了您的问题,您应该始终接受答案 :)
    • @rurtle :感谢您接受弗拉德的回答。我很高兴至少你做到了。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多