C 链表在开头打印额外的0

C 链表在开头打印额外的0,c,pointers,linked-list,malloc,singly-linked-list,C,Pointers,Linked List,Malloc,Singly Linked List,我有一个非常基本的单链表实现。但是,我的实现的问题是,它在列表的开头打印一个额外的零,而我没有显式地为这个额外的节点分配任何存储。其代码如下所示: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define LEN 7 /* List node data structure */ typedef struct _ll_node_ { int data; struct

我有一个非常基本的单链表实现。但是,我的实现的问题是,它在列表的开头打印一个额外的零,而我没有显式地为这个额外的节点分配任何存储。其代码如下所示:

#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;
}

您已经将内存分配给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;
}
intmain(intargc,char*argv[])
{
node*head=NULL;
int i=0;
/*填充列表*/
对于(;i
这句话

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;
}
#包括
#包括
#包括
#包括
#定义len7
/*列表节点数据结构*/
typedef结构节点{
int数据;
结构节点下一步;
}节点;
/*
*@打印列表状态的简短实用程序
*/
无效打印列表(节点*头)
{
int i=0;
节点*tmp=头部;
while(tmp)
{
printf(“节点:\t%d,\t值:\t%d\n”,++i,tmp->data);
tmp=tmp->next;
}
printf(“\n”);
}
/*
*@brief实用程序将节点添加到列表中
*/
int add_节点(节点**头部,int数据)
{
成功;
while(*head!=NULL)head=&(*head)->next;
*head=malloc(sizeof(node));
success=*head!=NULL;
如果(成功)
{
(*头部)->数据=数据;
(*head)->next=NULL;
}
回归成功;
}
/*
*@简短的驱动程序功能
*/
内部主(空)
{
node*head=NULL;
int i=0;
srand((无符号整数)时间(NULL));
/*填充列表*/
对于(;i
注释这两行
head=malloc(LEN*sizeof(node));断言(head!=NULL)和其他一切都很好。感谢莫斯科的@vlad和Priyans goel指出这一点。完全错过了它。@rurtle:更好的感谢方式是接受答案。@rurtle:我并不迫切需要这样的分数。任何适合你的答案都应该被接受。我不介意你接受弗拉德的回答只是,如果答案能解决你的问题,你应该始终接受它:)@rurtle:谢谢你接受弗拉德的答案。至少你做到了,我很高兴。
#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;
}