Challenge

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

Ans

//* Project 1: Program of mathemathical set theory(find union and intersection of two nodes using linklist *//

#include

#include

struct node         // A structure of linked list node

{

int data;

struct node *next;

} *LLamol, *LLraj, *unionLL, *intersectionLL;

void initialize()

{

LLamol = LLraj = NULL;

}

void insert(struct node **head, int num)    //Given a Inserts a node in front of a singly linked list//

{

struct node* newNode = (struct node*) malloc(sizeof(struct node));  // Create a new Linked List node

newNode->data = num;

newNode->next = *head;  // Next pointer of new node will point to head node of linked list

*head = newNode;    //make new node as new head of linked list

}

int compare(struct node *head, int num) // Searches an element in Linked List by linearly traversing from head to tail

{

while (head != NULL)

{

if (head->data == num)

{

return 1;

}

head = head->next;

}

return 0;

}

struct node* findunion(struct node *LLamol, struct node *LLraj)     // Returns the union of two given linked list

{

unionLL = NULL;

struct node *temp = LLamol; // Add all nodes of first Linked List to unionLL

while(temp != NULL)

{

insert(&unionLL, temp->data);

temp = temp->next;

}

while(LLraj != NULL)    // Insert those nodes of LLTwo which is not present in LLOne

{

if(!compare(LLamol, LLraj->data))

{

insert(&unionLL, LLraj->data);

}

LLraj = LLraj->next;

}

return unionLL;

}

struct node* intersection(struct node *LLamol, struct node *LLraj) //Returns the Linked List which contains common nodes of two given linked list

{

intersectionLL = NULL;

while(LLamol != NULL)   //Search every element of LLOne in LLTwo, If found then add it to intersection List

{

if(compare(LLraj, LLamol->data))

{

insert(&intersectionLL, LLamol->data);

}

LLamol = LLamol->next;

}

return intersectionLL;

}

void printLinkedList(struct node *nodePtr)  // Prints a linked list from head node till tail node

{

while (nodePtr != NULL)

{

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

nodePtr = nodePtr->next;

if(nodePtr != NULL)

printf("-->");

}

}

int main()

{

int i, LLamolCount, LLrajCount, temp;

initialize();

printf("Enter number of nodes in first Linked List\n"); //Creating First linked List

scanf("%d", &LLamolCount);

printf("Enter %d integers\n", LLamolCount);

for(i=0; i

{

scanf("%d", &temp);

insert(&LLamol, temp);

}

printLinkedList(LLamol);

printf("\nEnter number of nodes in second Linked List\n");

scanf("%d", &LLrajCount);

printf("Enter %d integers\n", LLrajCount);

for(i=0; i

{

scanf("%d", &temp);

insert(&LLraj, temp);

}

printLinkedList(LLraj);

findunion(LLamol, LLraj);   // printing Union of two given linked list

intersection(LLamol, LLraj);

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

printLinkedList(unionLL);

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

printLinkedList(intersectionLL);

return 0;

}

OUTPUT:

Enter number of nodes in first Linked List

2

Enter 2 integers

Enter number of nodes in second Linked List

5

Enter 5 integers

10

22

45

75

95

95-->75-->45-->22-->10

Union Linked List

10-->22-->45-->75-->95

Intersection Linked List

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.