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
    • 1.90k
    Comment on it

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

    /* 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);
    /* push function to add elements into linked list */
    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*/
    	new_node->next = (*head_ref);
    	/* head will contain address of new node */
    	(*head_ref) = new_node;
    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;




    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)

Sign In

Sign up using

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