C Program to Find the First Common Element in Two Linked Lists

«
»
This C Program finds the first common element between the 2 given linked list.

Here is a source code of the C Program to find the first common element between the 2 given 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 Find the first Common Element between the 2 given Linked Lists 
  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 find(struct node *, struct node *);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL, *q = NULL;
  21.     int result;
  22.  
  23.     printf("Enter data into the list\n");
  24.     create(&p);
  25.     printf("Enter data into the list\n");
  26.     create(&q);
  27.     printf("Displaying list1:\n");
  28.     display(p);
  29.     printf("Displaying list2:\n");
  30.     display(q);
  31.     result = find(p, q);
  32.     if (result)
  33.     {
  34.         printf("The first matched element is %d.\n", result);
  35.     }
  36.     else
  37.     {
  38.         printf("No matching element found.\n");
  39.     }
  40.     release (&p);
  41.  
  42.     return 0;
  43. }
  44.  
  45. int find(struct node *p, struct node *q)
  46. {
  47.     struct node *temp;
  48.  
  49.     while (p != NULL)
  50.     {
  51.         temp = q;
  52.         while (temp != NULL)
  53.         {
  54.             if (temp->num == p->num)
  55.             {
  56.                 return p->num;
  57.             }
  58.             temp = temp->next;
  59.         }
  60.         p = p->next;
  61.     }
  62.  
  63.     /*Assuming 0 is not used in the list*/
  64.     return 0;
  65. }
  66.  
  67. void create(struct node **head)
  68. {
  69.     int c, ch;
  70.     struct node *temp, *rear;
  71.  
  72.     do
  73.     {
  74.         printf("Enter number: ");
  75.         scanf("%d", &c);
  76.         temp = (struct node *)malloc(sizeof(struct node));
  77.         temp->num = c;
  78.         temp->next = NULL;
  79.         if (*head == NULL)
  80.         {
  81.             *head = temp;
  82.         }
  83.         else
  84.         {
  85.             rear->next = temp;
  86.         }
  87.         rear = temp;
  88.         printf("Do you wish to continue [1/0]: ");
  89.         scanf("%d", &ch);
  90.     } while (ch != 0);
  91.     printf("\n");
  92. }
  93.  
  94. void display(struct node *head)
  95. {
  96.     while (head != NULL)
  97.     {
  98.         printf("%d\t", head->num);
  99.         head = head->next;
  100.     }
  101.     printf("\n");
  102. }
  103.  
  104. void release(struct node **head)
  105. {
  106.     struct node *temp;
  107.     while ((*head) != NULL)
  108.     {
  109.         temp = *head;
  110.         (*head) = (*head)->next;
  111.         free(temp);
  112.     }
  113. }

$ cc firstcommon.c 
$ ./a.out
Enter data into the list1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 8
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 0
 
Enter data into the list2
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
 
Displaying list1:
2	8	5	6	
Displaying list2:
3	5	9	
The first matched element is 5.

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.

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.