C program to find the number of nodes present in the entered Linked List

PROGRAM:
#include<stdio.h>
struct node
 {
  int data;
  struct node *next;
 } *head,*temp,*temp1;
void create();
void find();
void print();
void main()
 {
   create();
   printf("\nElements in the entered list:\n");
   print();
   find();

 }
void create()
 {
   int num;
   printf("\nEnter element or enter -1 to exit\n");
   head=(struct node*)malloc(sizeof(struct node));
   scanf("%d",&num);
   if(num==-1)
     {
      head=NULL;
     }
   else
     {
       head->data=num;
       head->next=NULL;
     }
   while(head!=NULL)
     {
       printf("\nEnter element or enter -1 to exit\n");
       scanf("%d",&num);
       if(num==-1)
{break;}
       else
{
 temp=head;
 while(temp->next!=NULL)
   {
     temp=temp->next;
   }
 temp1=(struct node*)malloc(sizeof(struct node));
 temp1->data=num;
 temp1->next=NULL;
 temp->next=temp1;
}
     }
 }
/*Find function finds the number of nodes present in the entered linked list*/
void find()
 {
   int count;
   for(temp=head,count=0;temp!=NULL;temp=temp->next,count++);
   printf("\n\nEntered Linked list has %d nodes.",count);
 }
void print()
 {
   struct node *head1=head;
   while(head1!=NULL)
    {
     printf("%d\t",head1->data);
     head1=head1->next;
    }
 }

OUTPUT:
NUMBER-OF-NODES-IN-LINKED-LIST
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment