Challenge

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

Challenge #08

Project #01:

  • Creating two list of data using linked list method.
  • Insert the node to 1st and 2nd list accordingly.
  • Intersection of data of two lists.
  • Getting Union of two list.

#include

#include //for DMA - malloc function

struct Node //define a struct name node to represent node in a linked list

{

int data;

struct Node* next;

};

void insertAtEnd(struct Node** head, int data)

//Function to insert the node at the end of the list

{

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

//To ensure the memory allocated correctly and used for storing the data

//and to next pointer of the node

newNode ->data = data;

newNode ->next = NULL;

if(*head == NULL)

{

*head = newNode;

}

else

{

struct Node* current = *head;

while(current->next != NULL)

{

current = current ->next;

}

current->next = newNode;

}

}

void printList(struct Node* head)

//Function to print a linked list

{

struct Node* current = head;

while(current != NULL)

{

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

current = current->next;

}

printf("\n");

}

struct Node* intersection(struct Node* list1, struct Node* list2)

//Function to perform intersection of two linked lists

{

struct Node* result = NULL;

struct Node* current1 = list1;

while(current1 != NULL)

{

struct Node* current2 = list2;

while(current2 != NULL)

{

if(current1 ->data == current2->data)

{

insertAtEnd(&result, current1->data);

break; //Break after finding the common element

}

current2 = current2->next;

}

current1 = current1->next;

}

return result;

}

struct Node* unionList(struct Node* list1, struct Node* list2)

//Function to perform union of two linked lists

{

struct Node* result = NULL;

struct Node* current1 = list1;

struct Node* current2 = list2;

while(current1 != NULL)

{

insertAtEnd(&result,current1->data);

current1 = current1 ->next;

}

while(current2 != NULL)

//check if the result is not already in the result list

{

struct Node* temp = result;

int found = 0;

while(temp != NULL)

{

if(temp->data == current2->data)

{

found = 1;

break;

}

temp = temp ->next;

}

if(!found)

{

insertAtEnd(&result,current2->data);

}

current2 = current2->next;

}

return result;

}

int main()

{

struct Node* list1 = NULL;

struct Node* list2 = NULL;

int n, data;

//input for the first list

printf("Enter the number of elements for the list1: ");

fflush(stdout);

scanf("%d",&n);

printf("Enter the elements for list 1: \n");

for(int i=0;i

{

fflush(stdout);

scanf("%d",&data);

insertAtEnd(&list1,data);

}

//input for the second list

printf("Enter the number of elements for the list2: ");

fflush(stdout);

scanf("%d",&n);

printf("Enter the elements for list 2: \n");

for(int i=0;i

{

fflush(stdout);

scanf("%d",&data);

insertAtEnd(&list2,data);

}

printf("list1: ");

printList(list1);

printf("list2: ");

printList(list2);

//To calculate and print the intersection

struct Node* intersectionResult = intersection(list1,list2);

printf("intersection: ");

printList(intersectionResult);

//To calculate and print the union

struct Node* unionResult = unionList(list1,list2);

printf("union: ");

printList(unionResult);

//To free allocated memory

free(intersectionResult);

free(unionResult);

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.