12997 - Two Singly Linked List Comparison   

Description

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.

 

function.c

#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 = 0i < leni++)
    {
        int data;
        scanf("%d", &data);
        insert_node(listdata);
    }
 
    return list;
}
 
void insert_node(SinglyLinkedList *listint 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->tailtmp = 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 *list1SinglyLinkedList *list2)
{
    Node *tmp1 = list1->head;
    Node *tmp2 = list2->head;
 
    /* Todo */
}

Input

Input follows below format:

len1

n1 n2 n3 n4 ….

len2

m1 m2 m3 m4 ….

 

Note:

  1. All len1len2n1~ and m1~ are integers. len1 represents the length of the first linked list, also indicates the total number of the data in the next line.
  2. 32767 >= len1, len2 >= 0.
  3. 32767 >= n1~, m1~ >= -32768.

 

Example:

3

1 2 3

4

1 2 3 4

 

List1: 1 -> 2 -> 3 -> NULL

List2: 1 -> 2 -> 3 -> 4 -> NULL

 

These two lists have equal data attributes for the first nodes, List2 is longer, though, these lists are not equal.

 

Output

Output should follow below format:

1(0)

Note:

  1. Need to have a return value('\n') at the end of your string.
  2. If two lists are equal, print (“1\n”); otherwise, print(“0\n”).

Sample Input  Download

Sample Output  Download

Partial Judge Code

12997.c

Partial Judge Header

12997.h

Tags




Discuss