简体   繁体   English

如何计算链表中的节点数?为什么输出显示节点数为“2”?

[英]How to count number of nodes in a linked-list ?Why does the output show the count of nodes as '2'?

I have written ac program to find the number of nodes in a link list.我编写了 ac 程序来查找链接列表中的节点数。 But the problem arises because the value of count that I am printing comes out to be '2'.但是问题出现了,因为我打印的 count 的值是“2”。

My exact output looks like->我的确切输出看起来像->

NUMBER OF NODES ARE 2节点数为 2

What am I doing wrong here?我在这里做错了什么?

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

I think that in the loop within the function create我认为在函数中的循环中create

for(i=1;i<n;i++)

you mean你的意思是

last = last->next;

instead of代替

last=first;

Pay attention to that it is a bad idea to declare the pointer to the head node as global and when functions depend on global variables.请注意,当函数依赖于全局变量时,将指向头节点的指针声明为全局是一个坏主意。

Also the user can pass to the function the number of elements of an array equal to 0. Also allocation of memory can fail.此外,用户可以将等于 0 的数组元素数传递给函数。内存分配也可能失败。

I would declare the function the following way我会用以下方式声明函数

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

And call the function like并调用函数

size_t n = create( &first, a, sizeof( a ) / sizeof( *a ) );

In this case the function returns the number of created nodes in the list.在这种情况下,该函数返回列表中创建的节点数。

Here is a demonstrative program.这是一个演示程序。

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

Its output is它的输出是

There are 4 nodes in the list
They are 1 -> 2 -> 3 -> 4 -> null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM