## Q 9 B. Define the structure of a node of a singly linked list and use it to write a function to count thenumber of nodes in a singly linked list. The function should accept a pointer to the first node ofthe list and it should return the number of nodes in the list.

### Solution :

/*   ************************************************
Girfa : Student Help
for more program visit : http://girfahelp.blogspot.com/p/c-language-assignment.html
*************************************************/

typedef struct n
{
int data;
struct n *next;
}node;
/* Function Prototype */
node *start=0; /* Global Variable point first node*/
int count();
void disp();
#include<stdio.h>
#include<conio.h>
void main()
{
int opt,n;

do
{
clrscr();
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter  number to add in list>> ");
scanf("%d",&n);
break;
case 2:
printf("\nTotal %d node in link list>> ",count(start));
getch();
break;
case 3:
disp();
getch();
case 0:
break;
default:
printf("Invalid Choice");
getch();
}
}while(opt!=0);

}
int count()
{
node *pt;
int i=0;
for(pt=start;pt!=NULL;pt=pt->next)
i++;
return i;
}
{
node *nw=(node*) malloc(sizeof(node));
nw->next=NULL;
nw->data=n;
if(start==NULL)
start=nw;
else
{
nw->next=start;
start=nw;
}

}
void disp()
{
node *pt;
for(pt=start;pt!='\0';pt=pt->next)
printf("[%d]",pt->data);

}