Challenge

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

#include

#include

//creating a node

struct Node

{

int value;

struct Node *next;

};

//Initializing different list that will be used in this program

struct Node *list1, *list2, *list_union, *list_intersection;

//function to display list

void display_list(struct Node *p)

{

//checking whether list is empty

if(p == NULL)

{

printf("\nList is empty!!!");

}

//printing the list

else

{

while(p != NULL)

{

printf("%d", p->value);

p = p->next;

}

}

}

//new node creation

struct Node *newNode(int value)

{

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

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 exiting 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 value

if(list->value == data)

{

return 1;

}

//if not matched, point to next node address

list = list->next;

}

return 0;

}

struct Node *get_union(struct Node *L1, struct Node *L2)

{

//initializing union list as NULL

list_union = NULL;

struct Node *temp = L1;

//copying list 1 data to union list

while(temp != NULL)

{

insert_node(&list_union, newNode(temp->value));

temp = temp->next;

}

//coping 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 unique elements to union list

insert_node(&list_union, newNode(L2->value));

}

//if matched, point to next node address

L2 = L2->next;

}

return list_union;

}

struct Node *get_intersection(struct Node *L1, struct Node *L2)

{

//initialing 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;

}

return list_intersection;

}

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 < list2>

{

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 both list

get_union(list1, list2);

//display union of both list

printf("\nUnion of both list is : ");

display_list(list_union);

//display intersection of both list

get_intersection(list1, list2);

//performing intersection of both list

printf("\nIntersection of both list is : ");

display_list(list_intersection);

return 0;

}

Output :

Enter number of nodes in list 1 : 4

Enter 4 elements of list 1 : 1 2 3 5

Enter number of nodes in list 2 : 3

Enter 3 elements of list 2 : 4 5 5

List 1 is : 1235

List 2 is : 455

Union of both list is : 12345

Intersection of both list is : 5

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.