C Program to Search an Element in Linked List without using Recursion

«
»
This C program, using iteration, searches for an element in a linked list. A linked list is an ordered set of data elements, each containing a link to its successor.

Here is the 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 the Linked List without 
  3.  * using Recursion
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct node
  10. {
  11.     int a;
  12.     struct node *next;
  13. };
  14.  
  15. void generate(struct node **, int);
  16. void search(struct node *, int);
  17. void delete(struct node **);
  18.  
  19. int main()
  20. {
  21.     struct node *head = NULL;
  22.     int key, num;
  23.  
  24.     printf("Enter the number of nodes: ");
  25.     scanf("%d", &num);
  26.     printf("\nDisplaying the list\n");
  27.     generate(&head, num);
  28.     printf("\nEnter key to search: ");
  29.     scanf("%d", &key);
  30.     search(head, key);
  31.     delete(&head);
  32.  
  33.     return 0;
  34. }
  35.  
  36. void generate(struct node **head, int num)
  37. {
  38.     int i;
  39.     struct node *temp;
  40.  
  41.     for (i = 0; i < num; i++)
  42.     {
  43.         temp = (struct node *)malloc(sizeof(struct node));
  44.         temp->a = rand() % num;
  45.         if (*head == NULL)
  46.         {
  47.             *head = temp;
  48.             temp->next = NULL;
  49.         }
  50.         else
  51.         {
  52.             temp->next = *head;
  53.             *head = temp;
  54.         }
  55.         printf("%d  ", temp->a);
  56.     }
  57. }
  58.  
  59. void search(struct node *head, int key)
  60. {
  61.     while (head != NULL)
  62.     {
  63.         if (head->a == key)
  64.         {
  65.             printf("key found\n");
  66.             return;
  67.         }
  68.         head = head->next;
  69.     }
  70.     printf("Key not found\n");
  71. }
  72.  
  73. void delete(struct node **head)
  74. {
  75.     struct node *temp;
  76.  
  77.     while (*head != NULL)
  78.     {
  79.         temp = *head;
  80.         *head = (*head)->next;
  81.         free(temp);
  82.     }
  83. }

$ gcc search_iter.c -o search_iter
$ a.out
Enter the number of nodes: 10
 
Displaying the list
3  6  7  5  3  5  6  2  9  1  
Enter key to search: 2
key found

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 other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.

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 & technical discussions at Telegram SanfoundryClasses.