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

Archives

program to search a node by value

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 search(int);
 };

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

  void linklist::search(int a)
  { int count,count1;
    count=count1=0;
    node *p=start;
    while(p!=NULL)
    {  count1++;
       if(p->info==a)
       {  count++;
      cout<<"this value occured at "<<count1<<"th node"<<endl;
       }
    p=p->link;
    }
    if(count==0)
    cout<<"There is no node at this value"<<endl;

  }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(5);
     l1.insert(20);
     l1.insert(30);
     getch();
     l1.search(40);
     getch();
     l1.search(20);
     getch();
   }

0 comments: