c program to count number of nodes in linked list

c program to count the number of nodes in linked list. Linked list is a data structure. Linked list is made of collection of nodes, which consist of data field and linked field. While doubly Linked list contains two linked field and one data field. You can implement c program to perform many operation of insertion – deletion, reversing the order of linked list and counting the nodes in linked list.Here is the implementation of

c program to count number of nodes in linked list :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
}*head,*temp;

void insert_data(int value)
{
    struct node *var;
    temp=head;
    var=(struct node *)malloc(sizeof(struct node));
    var->data=value;

    if(head==NULL)
    {
         head=var;
         head->next=NULL;
    }
    else
    {
         while(temp->next!=NULL)
         {
              temp=temp->next;
         }
         var->next=NULL;
         temp->next=var; 
    }
}

int count_node()
{
     int i=0;
     temp=head;
     while(temp!=NULL)
     {
          i++;
          temp=temp->next;
     }
     printf("\n\nnumber of nodes are %d  ",i);
}

void display()
{
     struct node *var;
     var=head;
     printf("\nlist of elments are \n");
     while(var!=NULL)
     {
          printf("-> %d ",var->data);
          var=var->next;
     }
}

int main()
{
     int i,value;
     char ch='y';
     head=NULL;

     printf(" 1.)  Insert node");
     printf("\n 2.)  display the list");
     printf("\n 3.)  count number of nodes");
     printf("\n 4.)  exit");
     while(ch=='y')
     {
          printf("\nChoose to do operation :");
          scanf("%d",&i);
          switch(i) 
          {
               case 1 :
               {
               printf("\nEnter the data to be inserted in node ");
               scanf("%d",&value);
               insert_data(value);
              //display();
               break;
          }
          case 2 :
          {
               display();
               break;
          }
          case 3 :
          {
               count_node();
               break;
          }
          case 4 :
          {
               exit(0);
               break;
          }
     }
}
getch();
}

Output of c program to count number of nodes in linked list:

c program to count number of nodes in linked list

 


One thought on “c program to count number of nodes in linked list”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>