My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to count number of nodes in the link list

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void count();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
      p=NULL;
     }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
    p=NULL;
      }
  }

  void linklist::count()
  { int count=0;
    node *p=start;
    while(p!=NULL)
    {  p=p->link;
       count++;
    }
    if(count==0)
    cout<<"there is no node";
    else
    cout<<"number of nodes are: "<<count;
  }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(30);
     l1.count();
     getch();
   }

0 comments: