C Program to Check if Two Lists are Equal

This C Program checks whether 2 lists are the same. The lists are said to be same if they contain same elements at same position.

Here is source code of the C Program to check whether 2 lists are the same. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Check whether 2 Lists are Same 
  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 feedmember(struct node **);
  14. int compare (struct node *, struct node *);
  15. void release(struct node **);
  16.  
  17. int main()
  18. {
  19.     struct node *p = NULL;
  20.     struct node *q = NULL;
  21.     int result;
  22.  
  23.     printf("Enter data into first list\n");
  24.     feedmember(&p);
  25.     printf("Enter data into second list\n");
  26.     feedmember(&q);
  27.     result = compare(p, q);
  28.     if (result == 1)
  29.     {
  30.         printf("The 2 list are equal.\n");
  31.     }
  32.     else
  33.     {
  34.         printf("The 2 lists are unequal.\n");
  35.     }
  36.     release (&p);
  37.     release (&q);
  38.  
  39.     return 0;
  40. }
  41.  
  42. int compare (struct node *p, struct node *q)
  43. {
  44.     while (p != NULL && q != NULL)
  45.     {
  46.         if (p->num != q-> num)
  47.         {
  48.             return 0;
  49.         }
  50.         else
  51.         {
  52.             p = p->next;
  53.             q = q->next;
  54.         }
  55.     }
  56.     if (p != NULL || q != NULL)
  57.     {
  58.         return 0;
  59.     }
  60.     else
  61.     {
  62.         return 1;
  63.     }
  64. }
  65.  
  66. void feedmember (struct node **head)
  67. {
  68.     int c, ch;
  69.     struct node *temp;
  70.  
  71.     do
  72.     {
  73.         printf("Enter number: ");
  74.         scanf("%d", &c);
  75.         temp = (struct node *)malloc(sizeof(struct node));
  76.         temp->num = c;
  77.         temp->next = *head;
  78.         *head = temp;
  79.         printf("Do you wish to continue [1/0]: ");
  80.         scanf("%d", &ch);
  81.     }while (ch != 0);
  82.     printf("\n");
  83. }
  84.  
  85. void release (struct node **head)
  86. {
  87.     struct node *temp = *head;
  88.  
  89.     while ((*head) != NULL)
  90.     {
  91.         (*head) = (*head)->next;
  92.         free(temp);
  93.         temp = *head;
  94.     }
  95. }

$ cc checklinklist.c 
$ ./a.out
Enter data into first list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 28
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
 
Enter data into second list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 28
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
 
The 2 list are equal.

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.