Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

NO OF NODES IN LINK LIST

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int getcount(struct node *nu)
{
    int cnt=0;
    struct node *current=nu;
    while(current!=NULL)
    {
        //cnt++;
        current=current->next;
        //nu->current=next;
        cnt++;
    }
    return cnt;
}
void add(struct node **ptr,int data)
{
    struct node *nu=NULL;
    nu=malloc(sizeof(struct node)) ;
    {
        if(nu==NULL)
        {
            printf("list is empty\n");
        }
//nu->data=ptr;
        /*nu->next=data;
        nu->next=ptr;
        nu->data=ptr;*/
        nu->data=data;
        nu->next=*ptr;
        *ptr=nu;
        
    }
}
    int main() 
{ 
        int cnt=0;
 
    struct node *nu = NULL; 
  
    add(&nu, 1); 
    add(&nu, 2); 
    add(&nu, 3); 
    add(&nu, 4); 
    add(&nu, 5); 
  
   
    printf("count of nodes is %d", getcount(nu)); 
   // return cnt; 
} 
 run  | edit  | history  | help 0