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.
/*
* C Program to Check whether 2 Lists are Same
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
void feedmember(struct node **);
int compare (struct node *, struct node *);
void release(struct node **);
int main()
{
struct node *p = NULL;
struct node *q = NULL;
int result;
printf("Enter data into first list\n");
feedmember(&p);
printf("Enter data into second list\n");
feedmember(&q);
result = compare(p, q);
if (result == 1)
{
printf("The 2 list are equal.\n");
}
else
{
printf("The 2 lists are unequal.\n");
}
release (&p);
release (&q);
return 0;
}
int compare (struct node *p, struct node *q)
{
while (p != NULL && q != NULL)
{
if (p->num != q-> num)
{
return 0;
}
else
{
p = p->next;
q = q->next;
}
}
if (p != NULL || q != NULL)
{
return 0;
}
else
{
return 1;
}
}
void feedmember (struct node **head)
{
int c, ch;
struct node *temp;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}while (ch != 0);
printf("\n");
}
void release (struct node **head)
{
struct node *temp = *head;
while ((*head) != NULL)
{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}
$ 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]Related Posts:
- Check Programming Books
- Check Data Structure Books
- Practice Design & Analysis of Algorithms MCQ
- Practice Programming MCQs
- Check Computer Science Books