Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
//....Defining the Node....
typedef struct Node
{
int data;
struct Node *Next;
} Node; //User define data typedef declaration
// Static Variables Declaration
static Node *temp1,*temp2,*First_node,*Second_node,*First_newnode,*Second_newnode,*find_intersection,*find_union,*newnode,*head_intersection,*head_union;
// This function is creating for inserting the elements to linked list
Node *insert_node(Node *head_ref,int n)
{
newnode=(Node*)malloc(sizeof(Node)); //allocation of memory
newnode->data = n;
newnode->Next=head_ref;
head_ref=newnode;
return head_ref;
}
// This function is creating for compare two different linked list
int compare(Node *head, int data)
{
while(head != NULL)
{
if(head->data==data)
return 1;
head=head->Next;
}
return 0;
}
// This function is creating for to get the Union elements
Node *get_union(Node *head_pointer1, Node *head_pointer2)
{
Node *temp = head_pointer1; //created for temporary value is equal to pointer 1
while(temp != 0) //check whether the head pointer is not equal to 0 or NULL
{
head_union=insert_node(head_union,temp->data);
temp=temp->Next;
}
while(head_pointer2 != 0)
{
if (!compare(head_pointer1, head_pointer2->data))
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->Next;
}
return head_union;
}
//This function is Creating for to get the Intersection elements //
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1 != 0)//check whether the head pointer is not equal to 0 or NULL//
{
if (compare(head_pointer2, head_pointer1->data))
head_intersection=insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->Next;
}
return head_intersection;
}
//Prints a linked list from first node to last node
void print_LinkedList(Node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->Next;
if(nodePtr != NULL)
printf("-->");
}
}
int main() //Main function
{
int x,y,i; // local variables Declaration
// Allocation of memory in heap.
temp1 = malloc(sizeof(Node));
temp2 = malloc(sizeof(Node));
// Asking to user to enter the number of elements of 1st linked list.
printf("Enter the Number of elements of First linked list\n");
fflush(stdout);
scanf("%d",&x);
// Asking to user to enter the items of 1st linked list.
printf("Enter the items of First linked list:\n");
fflush(stdout);
scanf("%d",&temp1->data);
temp1->Next = NULL;
First_node = temp1; // Creating 1st linked list
for(i = 1; i < x; i++)
{
First_newnode = malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(First_newnode->data));
First_newnode->Next = NULL;
First_node->Next = First_newnode;
First_node = First_node->Next;
}
First_node = temp1; // Printing the elements of 1st linked list
printf("You entered the First linked list:\n");
{
print_LinkedList(First_node);
}
// Asking user to enter the number of elements of 2nd linked list
printf("\nEnter the Number of elements of Second linked list\n");
fflush(stdout);
scanf("%d",&y);
// Asking to user to enter the items of 2nd linked list.
printf("Enter the items of second linked list:\n");
fflush(stdout);
scanf("%d",&temp2->data);
temp2->Next = NULL;
Second_node = temp2;
for(i = 1; i < y; i++) // Creating 2nd linked list
{
Second_newnode = malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(Second_newnode->data));
Second_newnode->Next = NULL;
Second_node->Next = Second_newnode;
Second_node = Second_node->Next;
}
Second_node = temp2; // Printing elements of 2nd linked list
printf("You entered Second linked list:\n");
{
print_LinkedList(Second_node);
}
// Union of both linked list
find_union = NULL;
{
printf("\nUnion of First and Second linked list\n");
}
find_union = get_union(First_node,Second_node);
print_LinkedList(head_union);
// Intersection of both linked list
find_intersection = NULL;
{
printf("\nIntersection of First and Second linked list\n");
}
find_intersection = get_intersection(First_node,Second_node); // calling get_intersection function
if(find_intersection == 0) //if there is no common items in both list
{
printf("First and Second linked list not have common items");
}
print_LinkedList(head_intersection);
return 0;
}