Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
1.Project
#include
#include
struct Node//creating node
{
int value;// creating the data type of the variable
struct Node* next;//pointer pointing towards next node
};
//initializing different list that will be used in this program
struct Node *list1, *list2, *list_union, *list_intersection;
void display_list(struct Node *p)//function used to display the nodes data
{
if (p == NULL)
{
printf("\nList is empty!!!");
}
else
{
while(p != NULL)
{
printf("%d", p->value);// priints the value of current node
p= p->next;//// advances the position of current node
}
}
}
//new Node creation
struct Node *newNode(int value)
{
struct Node *newNode =(struct Node*)malloc(sizeof(struct Node));//allocating the memory to the nodes
newNode->value = value;
newNode->next = NULL;
return newNode;
}
//Node insertion in sorted linked list
void insert_node(struct Node **head,struct Node *newNode)
{
//inserting node when list is empty
if(*head == NULL || (*head)->value >= newNode->value)
{
newNode->next = *head;
*head = newNode;
}
//Inserting node in between existing node
else
{
struct Node *current = *head;
while (current->next != NULL && current->next->value < newNode>value)
{
current=current->next;
}
newNode->next =current->next;
current->next =newNode;
}
}
//function to compare two nodes
int compare (struct Node *list,int data)
{
while (list != NULL)
{
//comparing Values
if(list->value == data)
{
return 1;
}
//if not matched,point to the node address
list = list->next;
}
return 0;
}
struct Node *get_union(struct Node *L1, struct Node *L2)
{
// link *list_union =(struct link*)malloc(sizeof(struct link));
list_union = NULL;
struct Node *temp=L1;
//copying list 1 data to union list
while(temp != NULL)
{
insert_node(&list_union,newNode(temp->value));//inserting the L1 elements to the newly created list
temp= temp->next;
}
//copying unique data elements of list 2 in union list
while (L2 != NULL)
{
//comparing list 1 and list 2 data elements
if(!compare(L1,L2->value))
{
//copying matched elements to union list
insert_node(&list_union,newNode(L2->value));
}
//if matched,point to next node address
L2=L2->next;
}
//returning union list
return list_union;
}
struct Node *get_intersection(struct Node *L1,struct Node *L2)
{
//initializing intersection list as NULL
list_intersection = NULL;
//comparing list 1 and list 2 data elements for same elements
while(L1 !=NULL)
{
//comparing list 1 and list 2 data elements
if(compare(L2,L1->value))
{
//copying matched elements to union list
insert_node(&list_intersection,newNode(L1->value));
}
//if matched,point to next node address
L1=L1->next;
}
//returning intersection list
return list_intersection;
}
//main function
//struct link *get_union(struct link *l1,struct link2)
int main()
{
//initializing list 1 and list 2 as NULL
list1 = NULL;
list2 = NULL;
//Declaring variables
int list1_nodes,list2_nodes;
int i,data;
//take list 1 nodes input from user
printf("\nEnter number of nodes in list 1:");
fflush(stdout);
scanf("%d",&list1_nodes);
//taking list 1 elements from user
printf("\nEnter �lements of list 1: ",list1_nodes);
for(i=0; i < list1>
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list1,newNode(data));
}
//take list 2 nodes input from user
printf("\nEnter number of nodes in list 2:");
fflush(stdout);
scanf("%d",&list2_nodes);
//taking list 2 elements from user
printf("\nEnter �lements of list 2: " ,list2_nodes);
for(i=0; i
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list2,newNode(data));
}
//display list 1
printf("\nList 1 is");
display_list(list1);
//display list 2
printf("\nList 2 is");
display_list(list2);
//performing union of two list
get_union(list1,list2);
//displaying union of both list
printf("\nUnion of both list is");
display_list(list_union);
//performing intersection of two list
get_intersection(list1,list2);
//displaying intersection of both list
printf("\nIntersectionion of both list is");
display_list(list_intersection);
//return value is o after executing the main fn
return 0;
}