Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• Check two Linked Lists are Identical or not in C

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 654
Comment on it

Program to check two Linked Lists are Identical or not in C.

``````#include<stdio.h>
#include<stdlib.h>

/* Structure for a linked list node */
struct node
{
int data;
struct node *next;
};

/* Returns true if linked lists a and b are identical,
otherwise false */
int areIdentical(struct node *x, struct node *y)
{
while (x != NULL && y != NULL)
{
if (x->data != y->data)
return 0;

/* If we reach here, then a and b are not NULL and
their data is same, so move to next nodes in both
lists */
x = x->next;
y = y->next;
}

// If linked lists are identical, then 'a' and 'b' must
// be NULL at this point.
return (x == NULL && y == NULL);
}

void push(struct node** head_ref, int new_data)
{
//creating a node
struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* put in the data */
new_node->data = new_data;

/* new node next will now point to head element*/

}

int main()
{
/* The constructed linked lists are :
a: 3->2->1
b: 3->2->1 */
struct node *x = NULL;
struct node *y = NULL;
push(&x, 1);
push(&x, 2);
push(&x, 3);
push(&y, 1);
push(&y, 2);
push(&y, 3);

areIdentical(x, y)? printf("Identical"):printf("Not identical");

return 0;
}
``````

Output:

`Identical`

Explanation:

In the above program a structure node with two fields data and next field is declared and defined for creating a node. From main function ,two LinkedList x and y are created and for each LinkedList push function is called. Push function is called with a data parameter and address of starting node and within push function a new node is created for calling linkedlist containing passed data in data field. After creating two LinkedList, function areIdentical is called to compare two linkelist whether they are identical or not. areIdentical function is called, x and y is passed as an argument to it. Within areIdentical function a while loop runs till either of the list is null and within loop condition is checked if data linkedlist x and linkedlist y are not equal then 0 is returned otherwise list point to next field. Finally if while loop does not return 0 then function returns a int value by checking condition whether two list have become null if condition true function return non-zero integer else 0. In main ternary operator is used for areIdentical(x, y) i.e if this function returs a true value then Identical is printed else Not Identical.

0 Comment(s)

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General