Given two linked lists. Please Write a C program to compare the data in the nodes of the linked lists to check if they are equal.
#include "function.h"
/************************ Node ************************/
Node *create_node(int data)
{
Node *node = (Node *)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
/************************ SLL ************************/
SinglyLinkedList *create_singlyLinkedList()
{
SinglyLinkedList *list = (SinglyLinkedList *)malloc(sizeof(SinglyLinkedList));
list->head = NULL;
list->tail = NULL;
int len;
scanf("%d", &len);
for (int i = 0; i < len; i++)
{
int data;
scanf("%d", &data);
insert_node(list, data);
}
return list;
}
void insert_node(SinglyLinkedList *list, int data)
{
Node *node = create_node(data);
if (list->head == NULL)
{
list->head = node;
}
else
{
list->tail->next = node;
}
list->tail = node;
}
void print_list(SinglyLinkedList *list)
{
if (list->head == NULL)
{
printf("Nothing in the linked list.\n");
return;
}
Node *tmp = list->head;
for (; tmp != list->tail; tmp = tmp->next)
{
printf("%d ", tmp->data);
}
printf("%d\n", list->tail->data);
}
void free_list(SinglyLinkedList *list)
{
Node *node = list->head;
while (node)
{
Node *tmp = node;
node = node->next;
free(tmp);
}
}
// Complete the compare_lists function below.
int compare_lists(SinglyLinkedList *list1, SinglyLinkedList *list2)
{
Node *tmp1 = list1->head;
Node *tmp2 = list2->head;
/* Todo */
}