Page 260 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 260

Unit 14: Searching




                    /* if we went through the entire list and didn’t find the                   Notes
                      * value, then return NULL signifying that the value wasn’t
          found
                    */
                   return NULL;
          }


                 Example:
          Write a C program to search an element in a given list of elements using Liner Search.
          /*c program for linear search*/
          #include<stdio.h>
          #include<conio.h>
          int main()
          {
            int arr[20];
            int i,size,sech;
            printf(“\n\t— Linear Search —\n\n”);
            printf(“Enter total no. of elements:”);
            scanf(“%d”,&size);
            for(i=0; i<size; i++)
            {
                printf(“Enter %d element:”, i+1);
                scanf(“%d”,&arr[i]);
            }
            printf(“Enter the element to be searched:”);
            scanf(“%d”,&sech);
            for(i=0; i<size; i++)
            {
                if(sech==arr[i])
              {
                  printf(“Element exits in the list at position: %d”, i+1);
                 break;
              }
            }
            getch();
            return 0;
          }










                                           LOVELY PROFESSIONAL UNIVERSITY                                   253
   255   256   257   258   259   260   261   262   263   264   265