C 如何计算链表中的节点数?为什么输出将节点数显示为';2';?

C 如何计算链表中的节点数?为什么输出将节点数显示为';2';?,c,data-structures,struct,linked-list,singly-linked-list,C,Data Structures,Struct,Linked List,Singly Linked List,我已经编写了一个c程序来查找链接列表中的节点数。但是问题出现了,因为我正在打印的count的值是'2' 我的确切输出看起来像-> 节点数为2 我做错了什么 //the code for the program is here:- #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*first=NULL; void create(int a[],int

我已经编写了一个c程序来查找链接列表中的节点数。但是问题出现了,因为我正在打印的count的值是'2'

我的确切输出看起来像->

节点数为2

我做错了什么

//the code for the program is here:-

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

struct node
{
 int data;
 struct node *next;
}*first=NULL;

void create(int a[],int n)
{
 struct node *t,*last;
 first=(struct node *)malloc(sizeof(struct node));
 first->data=a[0];
 first->next=0;
 last=first;
 int i;
 for(i=1;i<n;i++)
 {
  t=(struct node *)malloc(sizeof(struct node));
  t->data=a[i];
  t->next=NULL;
  last->next=t;
  last=first;
 }
}

void count(struct node *p) //function to count number of nodes
{
 int count=0;
 while(p!=NULL)
 {
  count++;
  p=p->next;
 }
 printf("Number of nodes are %d ",count);
}

int main()
{
 int a[]={1,2,3,4};
 create(a,4);
 count(first);
 return 0;
}
//程序的代码如下:-
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
}*第一个=空;
无效创建(int a[],int n)
{
结构节点*t,*last;
first=(结构节点*)malloc(sizeof(结构节点));
第一->数据=a[0];
第一个->下一个=0;
最后=第一;
int i;
对于(i=1;idata=a[i];
t->next=NULL;
last->next=t;
最后=第一;
}
}
void count(struct node*p)//计算节点数的函数
{
整数计数=0;
while(p!=NULL)
{
计数++;
p=p->next;
}
printf(“节点数为%d”,计数);
}
int main()
{
int a[]={1,2,3,4};
创建(a,4);
计数(第一);
返回0;
}

我认为在函数
的循环中创建

for(i=1;i<n;i++)
而不是

last=first;
请注意,当函数依赖于全局变量时,将指向head节点的指针声明为全局是个坏主意

用户还可以向函数传递等于0的数组元素数。内存分配也可能失败

我将按照以下方式声明该函数

size_t create( struct node **head, const int a[], size_t n )
{
    // if the list is not empty free its nodes
    while ( *head != NULL )
    {
        struct node *current = *head;
        *head = ( *head )->next;
        free( current );
    }

    size_t i = 0;

    for ( ; i < n && ( *head = malloc( sizeof( struct node ) ) ) != NULL; i++ )
    {
        ( *head )->data = a[i];
        ( *head )->next = NULL;

        head = &( *head )->next;
    }

    return i;
}
在这种情况下,函数返回列表中创建的节点数

这是一个演示程序

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

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

size_t create( struct node **head, const int a[], size_t n )
{
    // if the list is not empty free its nodes
    while ( *head != NULL )
    {
        struct node *current = *head;
        *head = ( *head )->next;
        free( current );
    }

    size_t i = 0;

    for ( ; i < n && ( *head = malloc( sizeof( struct node ) ) ) != NULL; i++ )
    {
        ( *head )->data = a[i];
        ( *head )->next = NULL;

        head = &( *head )->next;
    }

    return i;
}

void output( const struct node *head )
{
    for ( ; head != NULL; head = head->next )
    {
        printf( "%d -> ", head->data );
    }

    puts( "null" );
}

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

    int a[] = { 1, 2, 3, 4 };
    const size_t N = sizeof( a ) / sizeof( *a );

    size_t n = create( &head, a, N );

    printf( "There are %zu nodes in the list\n", n );

    printf( "They are " );

    output( head );

    return 0;
}

OT:使用可变宽度字体时,缩进宽度为1个字符的字符将丢失。建议每个缩进级别使用4个空格OT:关于:
first=(struct node*)malloc(sizeof(struct node));
t=(struct node*)malloc(sizeof(struct node));
1)在C中,返回的类型是
void*
,可以分配给任何指针。强制转换只会使代码混乱。建议去掉石膏。2) 调用任何堆分配函数时:
malloc()
calloc()
和/或
realloc()
始终检查(!=NULL)返回值以确保操作成功。3) 在退出程序之前,分配的内存未传递到
free()
,导致内存泄漏:关于:
struct node{int data;struct node*next;}*first=NULL
不要隐藏指针。而是将结构定义与结构实例分开。这使得代码更加清晰,灵活性更强
size_t n = create( &first, a, sizeof( a ) / sizeof( *a ) );
#include <stdio.h>
#include <stdlib.h>

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

size_t create( struct node **head, const int a[], size_t n )
{
    // if the list is not empty free its nodes
    while ( *head != NULL )
    {
        struct node *current = *head;
        *head = ( *head )->next;
        free( current );
    }

    size_t i = 0;

    for ( ; i < n && ( *head = malloc( sizeof( struct node ) ) ) != NULL; i++ )
    {
        ( *head )->data = a[i];
        ( *head )->next = NULL;

        head = &( *head )->next;
    }

    return i;
}

void output( const struct node *head )
{
    for ( ; head != NULL; head = head->next )
    {
        printf( "%d -> ", head->data );
    }

    puts( "null" );
}

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

    int a[] = { 1, 2, 3, 4 };
    const size_t N = sizeof( a ) / sizeof( *a );

    size_t n = create( &head, a, N );

    printf( "There are %zu nodes in the list\n", n );

    printf( "They are " );

    output( head );

    return 0;
}
There are 4 nodes in the list
They are 1 -> 2 -> 3 -> 4 -> null