C Program to Search an Element in a Linked List

This C Program to search for an element in a linked list.

Here is a source code of the C program to search for an element in a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Search for an Element in a Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. int search(struct node *, int);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL;
  21.     int key, result;
  22.  
  23.     printf("Enter data into the list\n");
  24.     create(&p);
  25.     printf("Displaying the nodes in the list:\n");
  26.     display(p);
  27.     printf("Enter key to search in the list: ");
  28.     scanf("%d", &key);
  29.     result = search(p, key);
  30.     if (result)
  31.     {
  32.         printf("%d found in the list.\n", key);
  33.     }
  34.     else
  35.     {
  36.         printf("%d not found in the list.\n", key);
  37.     }
  38.     release(&p);
  39.  
  40.     return 0;
  41. }
  42.  
  43. int search(struct node *head, int key)
  44. {
  45.     while (head != NULL)
  46.     {
  47.         if (head->num == key)
  48.         {
  49.             return 1;
  50.         }
  51.         head = head->next;
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
  57. void create(struct node **head)
  58. {
  59.     int c, ch;
  60.     struct node *temp, *rear;
  61.  
  62.     do
  63.     {
  64.         printf("Enter number: ");
  65.         scanf("%d", &c);
  66.         temp = (struct node *)malloc(sizeof(struct node));
  67.         temp->num = c;
  68.         temp->next = NULL;
  69.         if (*head == NULL)
  70.         {
  71.             *head = temp;
  72.         }
  73.         else
  74.         {
  75.             rear->next = temp;
  76.         }
  77.         rear = temp;
  78.         printf("Do you wish to continue [1/0]: ");
  79.         scanf("%d", &ch);
  80.     } while (ch != 0);
  81.     printf("\n");
  82. }
  83.  
  84. void display(struct node *p)
  85. {
  86.     while (p != NULL)
  87.     {
  88.         printf("%d\t", p->num);
  89.         p = p->next;
  90.     }
  91.     printf("\n");
  92. }
  93.  
  94. void release(struct node **head)
  95. {
  96.     struct node *temp = *head;
  97.     *head = (*head)->next;
  98.     while ((*head) != NULL)
  99.     {
  100.         free(temp);
  101.         temp = *head;
  102.         (*head) = (*head)->next;
  103.     }
  104. }

$ cc searchlist.c 
$ ./a.out 
Enter data into the list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 24
Do you wish to continue [1/0]: 1
Enter number: 36
Do you wish to continue [1/0]: 1
Enter number: 69
Do you wish to continue [1/0]: 1
Enter number: 21
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
12	4	24	36	69	21	
Enter key to search in the list: 36
36 found in the list.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.