Compare two lists in C# and get differences

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.

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

Next Steps:

  • Get Free Certificate of Merit in Data Structure I
  • Participate in Data Structure I Certification Contest
  • Become a Top Ranker in Data Structure I
  • Take Data Structure I Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Compare two lists in C# and get differences

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.

How to compare two linked list in C?

We'll compare these linked lists using the check_equal() function. The check_equal() function traverses the linked lists until at least one of them reaches to NULL (end). If the value fields of two linked list are not equal at any point, the function returns 0 (not equal).

How do you compare data in two linked lists?

Check if Linked-Lists are identical using linear traversal:.
Traverse both the linked lists simultaneously. If the data of the current node for one linked list is not equal to the node of the other one, then return false..
Return true, as both the linked lists are identical..

How to compare two items in a list C#?

Equals(Object) Method which is inherited from the Object class is used to check if a specified List<T> object is equal to another List<T> object or not. Syntax: public virtual bool Equals (object obj);

Can we compare two linked list?

Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicographically greater.