Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node* next;
  7. };
  8.  
  9. struct node *head;
  10.  
  11. int main()
  12. {
  13.     head=NULL;
  14.     struct node* temp1=(struct node*)malloc(sizeof(struct node));
  15.     temp1->data=3;
  16.     temp1->next=NULL;
  17.  
  18.     struct node* temp2=(struct node*)malloc(sizeof(struct node));
  19.     temp2->data=3;
  20.     temp2->next=NULL;
  21.     temp1->next=temp2;
  22.  
  23.     struct node* temp3=(struct node*)malloc(sizeof(struct node));
  24.     temp3->data=30;
  25.     temp3->next=NULL;
  26.     temp2->next=temp3;
  27.    
  28.     //adding another
  29.    
  30.     struct node* temp4=(struct node*)malloc(sizeof(struct node));
  31.     temp4->data=356;
  32.     temp4->next=temp3;
  33.     temp2->next=temp4;
  34.  
  35.     head=temp1;
  36.  
  37.     struct node* tempX;
  38.     tempX=head;
  39.  
  40.     int i,s,flag;
  41.     flag=0;
  42.     i=1;
  43.     printf("Enter the number you want to search: ");
  44.     scanf("%d",&s);
  45.     while (tempX!=NULL)
  46.     {
  47.         if (tempX->data==s)
  48.         {
  49.             printf("found in node %d\n",i);
  50.             flag=1;
  51.         }
  52.         i++;
  53.         tempX=tempX->next;
  54.     }
  55.     if (flag==0)
  56.     {
  57.         printf("Not found in the list\n");
  58.     }
  59.  
  60.     tempX=head;
  61.     i=1;
  62.     while (tempX!=NULL)
  63.     {
  64.         printf("Output is node %d %d\n",i,tempX->data);
  65.         tempX=tempX->next;
  66.         i++;
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement