Advertisement
rafid_shad

link list search an element

Oct 13th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     int a;
  7.     struct node *next;
  8. };
  9.  
  10. struct node* findata(struct node*temp,int x)
  11. {
  12.     int count=0;
  13.  
  14.     while(temp->a!=x && temp->next!=NULL)
  15.     {
  16.         count++;
  17.         temp=temp->next;
  18.  
  19.     }
  20.  
  21.     if(temp->a==x)
  22.     {
  23.         cout<<"fund at position "<<count+1 <<endl;
  24.  
  25.     }
  26.     else
  27.     {
  28.         cout<<"not found"<<endl;
  29.     }
  30. };
  31.  
  32. struct node *head=NULL,*temp;
  33.  
  34. int main()
  35. {
  36.     struct node *n1,*n2;
  37.     n1=(struct node*) malloc (sizeof(struct node));
  38.     cout<<"give information for n1"<<endl;
  39.     cin>>n1->a;
  40.     n1->next=NULL;
  41.     head=n1;
  42.     cout<<"give information for n2"<<endl;
  43.     cin>>n2->a;
  44.     n2->next=NULL;
  45.     n1->next=n2;
  46.  
  47.     int x;
  48.     cout<<"which data u want to find"<<endl;
  49.     cin>>x;
  50.     temp=head;
  51.  
  52.     findata(temp,x);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement