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)
{
current=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=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));
}
|
run
| edit
| history
| help
|
0
|
|
|