Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

set theory ( union ,intersection)

UNION (AUB) = 3,7,4,5,2,8 ( O/P - all given numbers assembled. but same numbers not repeated)

INTERSECTION (AnB) = 4,5 ( O/P - same numbers only assembled )

when Two linked list

  • For union: For every element in list-2 we check if that element is already present in the resultant list made using list-1.
    For intersection: For every element in list-1 we check if that element is also present in list-2.

------------------------------------------------------------------------------------------------------------------

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

// A structure of linked list node
struct node
{
int data;
struct node *next;


} *LL1, *LL2, *unionLL, *intersectionLL;


void initialize()

{
LL1 = LL2 = NULL;
}


void push (struct node **head, int num)

{
// Create a new Linked List 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;


//make new node as new head of linked list


*head = newNode;
}


// presents an element in Linked List by linearly traversing from head to tail


int present(struct node *head, int num)

{
while (head != NULL)

{
if (head->data == num)

{
return 1;
}
head = head->next;
}
return 0;

}

//Returns the union of two given linked list

struct node*getunion(struct node *LL1, struct node *LL2)

{
unionLL = NULL;


// Insert all elements of list 1
// list1 to the result list


while (LL1 != NULL)
{
push(&unionLL, LL1->data);
LL1 = LL1->next;
}

// Insert those elements of list2
// which are not present in result list


while (LL2 != NULL)
{
if (!present(unionLL, LL2->data))
push(&unionLL, LL2->data);
LL2 = LL2->next;
}

return unionLL;
}

//Returns the Linked List which contains common nodes of two given linked list

struct node* intersection(struct node *LL1, struct node *LL2)

{
intersectionLL = NULL;


//Search every element of LL1 in LL2, If found then add it to intersection List


while(LL1 != NULL)

{
if(present(LL2, LL1->data))
push(&intersectionLL, LL1->data);
LL1 = LL1->next;
}


return intersectionLL;
}


//Prints a linked list from head node till tail node

void printLinkedList(struct node *nodePtr)

{
while (nodePtr != NULL)

{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
printf("->");

}
}

int main()

{
int i, LL1set, LL2set, t;

initialize();


//Creating First linked List


printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LL1set);
printf("Enter %d integers\n", LL1set);
for(i=0; i<LL1set; i++)

{
scanf("%d", &t);
push(&LL1, t);
}
printLinkedList(LL1);

printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LL2set);
printf("Enter %d integers\n", LL2set);
for(i=0; i<LL2set; i++)

{
scanf("%d", &t);
push(&LL2, t);
}
printLinkedList(LL2);


//printing Union of two given linked list


getunion(LL1, LL2);


intersection(LL1, LL2);


printf("\nUnion Linked List\n");
printLinkedList(unionLL);


printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);

return 0;
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.