Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
LIVE BATCH 8 PROJECT 1
#include
#include
// Structure of Linkedlist node
struct node{
int data;
struct node *next;
}*LL1,*LL2,*unionLL,*intersectionLL;
void initialize()
{
LL1 = LL2 = NULL;
}
//Inserting node of LinkedList
void insert(struct node **head, int num)
{
//Create a new LinkedList node
struct node* newNode = (struct node*) malloc (sizeof(struct node));
newNode->data = num;
//next pointer of new node will point to head node of linked list
newNode->next = *head;
//Making newnode as new head of linkedlist
*head = newNode;
}
// Searching element by traversing from head to tail
int search(struct node *head,int num)
{
while (head!=NULL)
{
if(head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
//Finding union of two linkedlist
struct node* findunion(struct node *LL1, struct node *LL2)
{
unionLL = NULL;
//All node of first linkedlist is added to unionLL
struct node *temp = LL1;
while(temp !=NULL)
{
insert(&unionLL,temp->data);
temp = temp->next;
}
//Insert nodes that is not present in LL1
while(LL2 !=NULL)
{
if(!search(LL1,LL2->data))
{
insert(&unionLL,LL2->data);
}
LL2 = LL2->next;
}
return unionLL;
}
//Intersection of 2 Linked list
struct node* intersection(struct node *LL1, struct node *LL2)
{
intersectionLL = NULL;
//search element of LL1 in LL2 and then add it to intersectionLL
while(LL1 !=NULL)
{
if(search(LL2,LL1->data))
{
insert(&intersectionLL,LL1->data);
}
LL1 = LL1->next;
}
return intersectionLL;
}
//Print Linkedlist
void printLinkedList(struct node *ptr)
{
while(ptr != NULL)
{
printf("%d",ptr->data);
ptr = ptr->next;
if(ptr != NULL)
printf("-->");
}
}
//MAIN FUNCTION
int main()
{
int i,LL1count,LL2count,temp;
initialize();
//creating first linkedlist
printf("Enter the number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d",&LL1count);
printf("Enter the %d integers\n",LL1count);
fflush(stdout);
for(i=0;i<LL1count;i++)
{
scanf("%d", &temp);
insert(&LL1, temp);
}
printLinkedList(LL1);
//creating second linkedlist
printf("\nEnter the number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d", &LL2count);
printf("Enter the %d integers\n",LL2count);
fflush(stdout);
for(i=0;i<LL2count;i++)
{
scanf("%d",&temp);
insert(&LL2,temp);
}
printLinkedList(LL2);
//Printing union and intersection of 2 LinkedList
findunion(LL1,LL2);
intersection(LL1,LL2);
printf("\nUnion LinkedList\n");
printLinkedList(unionLL);
printf("\nIntersection LinkedList\n");
printLinkedList(intersectionLL);
return 0;
}
OUTPUT
Enter the number of nodes in first Linked List
5
Enter the 5 integers
4 5 7 8 2
2-->8-->7-->5-->4
Enter the number of nodes in second Linked List
5
Enter the 5 integers
7 8 3 6 9
9-->6-->3-->8-->7
Union LinkedList
3-->6-->9-->4-->5-->7-->8-->2
Intersection LinkedList
7-->8