SOIKAT

Untitled

Feb 25th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5.  
  6.     int data;
  7.     struct node *next;
  8.  
  9. };
  10. struct node *head;
  11. void Insert(int data,int pos)
  12. {
  13.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  14.     temp->data=data;
  15.     temp->next=NULL;
  16.     if(pos==1)
  17.     {
  18.  
  19.         temp-> next=head;
  20.         head=temp;
  21.         return;
  22.     }
  23.     struct node *temp1=head;
  24.     int i;
  25.     for(i=0; i<pos-2; i++)
  26.     {
  27.         temp1=temp1->next;
  28.     }
  29.     temp->next = temp1->next;
  30.     temp1->next = temp;
  31. }
  32. void print()
  33. {
  34.     struct node *temp=head;
  35.     while(temp!=NULL)
  36.     {
  37.         printf("%d ",temp->data);
  38.         temp = temp->next;
  39.     }
  40. }
  41.  
  42. int search(int value)
  43. {
  44.     struct node* temp1=head;
  45.     int f=0;
  46.     while (temp1!=NULL)
  47.     {
  48.         if(temp1->data == value)
  49.         {
  50.             f=1;
  51.         }
  52.         temp1=temp1->next;
  53.     }
  54.     return f;
  55. }
  56.  
  57. int main()
  58. {
  59.     head = NULL;
  60.     Insert(4,1);
  61.     Insert(5,1);
  62.     Insert(6,1);
  63.     print();
  64.     printf("\n");
  65.     Insert(7,2);
  66.     print();
  67.     printf("\n");
  68.     int f;
  69.     f=search(4);
  70.     if(f==1)
  71.         printf("Found");
  72.     else
  73.         printf("\n Not Found");
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment