Wednesday 11 February 2015

Program to count the total no. of nodes between first & last node of the list.



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
      int data;
      struct node *next;
}node;
main()
{
      node *head,*p,*head1,*q;
      int n,i,j,count=0;
      printf("N=");
      scanf("%d",&n);
      head=(node *)malloc(sizeof(node));
      scanf("%d",&head->data);
      p=head;
      for(i=1;i<n;i++)
      {
            p->next=(node *)malloc(sizeof(node));
            p=p->next;
            scanf("%d",&p->data);
            p->next=NULL;
      }
      p=head;
      printf("\n\nThe linked list is=\n\nHead");
      while(p!=NULL)
      {
            printf("->%d",p->data);
            p=p->next;
      }
      p=head->next;
      while(p->next!=NULL)
      {
            count++;
            p=p->next;
      }
      printf("\n\nThe no. of nodes between 1st & last node is=%d",count);
      getch();

}

No comments:

Post a Comment