Two unsorted linked lists are given.
Find the union.
List1:: 2->3->4->2->3->3->55
List2:: 1->3->55->100

Result:
2->3->4->55->1->100
The elements in the result can be in any order

List1 can be enmpty or 
List2 can be empty or
List1 and List2 both can be empty

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

#define FALSE 0
#define TRUE 1
#define MAX_HASH 20
#define DUMMY_VAL -100
typedef struct node_tag
{
	int data;
	struct node_tag *next;
}node_type;

enum
{
	SUCCESS=2,
	FULL,
	DUPLICATE
};


int hash_table[MAX_HASH];

void print_link_list(node_type *root);
node_type *create_link_list(int n);
void initialize_hash_table(void);
int hash_function(int key);
int insert_key_in_hash_table(int key);
void print_hash_table(void);
node_type *union_of_two_unsorted_link_list(node_type *head1 , node_type *head2);

void main()
{
	int n;
	node_type *head1 , *head2;
	node_type *head3;
	printf("Give the number of node in head1\n");
	scanf("%d",&n);
	head1=create_link_list(n);
	print_link_list(head1);

	printf("Give the number of node in head2\n");
	scanf("%d",&n);
	head2=create_link_list(n);
	print_link_list(head2);
	initialize_hash_table();
	head3=union_of_two_unsorted_link_list(head1 , head2);
	printf("The list After hashing\n");
	print_link_list(head3);
	
}

node_type *union_of_two_unsorted_link_list(node_type *head1 , node_type *head2)
{
	node_type *temp;
	node_type *node_to_be_deleted;
	node_type *head;
	int ret_val=SUCCESS;

	if(head1)
	{
		head=head1;
		temp=head1;
	}
	else
	{
		head=head2;
		temp=head2;
	}

	if(temp)
	{
		insert_key_in_hash_table(temp->data);
		while(temp->next)
		{
			ret_val=insert_key_in_hash_table(temp->next->data);
			if(ret_val==DUPLICATE)
			{
				node_to_be_deleted=temp->next;
				temp->next=node_to_be_deleted->next;
				free(node_to_be_deleted);
			}
			else
			{
				temp=temp->next;
			}
		}

		if(head !=head2)
		{
			temp->next=head2;
			while(temp->next)
			{
				ret_val=insert_key_in_hash_table(temp->next->data);
				if(ret_val==DUPLICATE)
				{
					node_to_be_deleted=temp->next;
					temp->next=node_to_be_deleted->next;
					free(node_to_be_deleted);
				}
				else
				{
					temp=temp->next;
				}
			}
		}
	}
	return head;
}



void initialize_hash_table(void)
{
	int i;
	for(i=0;i<MAX_HASH ; i++)
	{
		hash_table[i]=DUMMY_VAL;
	}
}

int hash_function(int key)
{
	int index;
	index=key%MAX_HASH;
	return index;
}

int insert_key_in_hash_table(int key)
{
	int index;
	int old_index;
	int dup=FALSE;
	int ret_val=SUCCESS;

	index=hash_function(key);
	old_index = index;

	if(hash_table[index]==key)
	{
		dup=TRUE;
	}
	else if(hash_table[index]!=DUMMY_VAL)
	{
		index=(old_index+1)%MAX_HASH;
	}
		
	while(hash_table[index]!=DUMMY_VAL && index!=old_index && dup == FALSE)
	{
		if(hash_table[index]==key)
		{
			dup = TRUE;
		}
		index = (index+1) % MAX_HASH;
	}
	if(hash_table[index]==DUMMY_VAL)
	{
		ret_val = SUCCESS;
		hash_table[index]=key;
	}
	else if(dup==TRUE)
	{
		ret_val=DUPLICATE;
	}
	else
	{
		ret_val = FULL;
	}

	return ret_val;
}


void print_link_list(node_type *root)
{
	node_type *temp;
	for(temp=root;root && temp->next;temp=temp->next)
	{
		printf("%d-->",temp->data);
	}
	if(temp)
	{
		printf("%d\n",temp->data);
	}
}


node_type *create_link_list(int n)
{
	int i,a[400];
	node_type *head,*temp,*root;
	head = NULL;
	root=NULL;
	printf("Give the link list elements\n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		temp=(node_type *)malloc(sizeof(node_type));
		temp->data=a[i];
		temp->next=NULL;
		
		if(i==0)
		{
			root=temp;
			head=temp;
		}
		else
		{
			head->next=temp;
			head=head->next;
		}
	}
	return root;
}

Free Web Hosting