Challenge

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

Description of Mathematical set theory :

The set theory consists of many set operations but here we are performing union and intersection operations, given that we have to perform these operations on linked lists. So here we are creating a structure, it has a collection of different data types. Here is the process to create two linked lists using the reference address. The main concept of linked lists is self-referential structure.

Implementation of C interface for mathematical set theory using C programming :

First, we are creating structure, in that we are creating two data types int for storing integer values, and pointer variable is used to store the address of the same structure itself and declaring different variables in the structure type, then we are creating a main function to get the values of list1 and list2, using for loop and using the function call in that.

Creation of two lists using the linked list method :

First, we are getting the size of the list, and using for loop we are inserting the data inside it, we use the insert function with arguments head address, and value from the user. Inside the insert function, we are inserting a node, in the inserted node we are creating the size of the list using the malloc function nothing but allocating memory to the nodes. In the data field, the data is entered given by the user, and storing the address in the head pointer, nothing but in the link field the address of the next node is stored and we are storing the new node address in the head for the next update. In this way, nodes are inserted and linked lists are created.

Getting Union of two linked lists :

Here we get a function call of get union with arguments of list_one and list_two. In this function, we are using the temp variable as a list and assigning all values of list1 to it and by using the while condition and insert function we are inserting all values present in the temp list to the union list, then we are comparing list1 and list2 values by calling compare function and we are inserting the list2 values which are not present in the list1 to the union list. In this way, we are obtaining the union of two linked lists.

Getting Intersection of two linked lists :

Here, we get a function call of get intersection with arguments of list_one and list_two. In this function, we compare both lists and return the values which are placed commonly. We are calling compare function in the if-command and comparing both lists and inserting common values into the intersection list by using the insert function.

Display function :

This function is used to display the data of different lists by using different arguments passing to the display function. Here to display linked list1, linked list2, union list, and intersection list we are calling the display function.

C-Code :

#include    // pre-processor

#include

// defining node using structure

struct node

{

int data ;

struct node *next ;

} *List_one, *List_two, *Union_list, *Intersection_list; 

// initializing list1 and list2 with NULL

void initialize ()

{

List_one = List_two = NULL ;

}

// inserting a node

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

{

struct node *new_node;

new_node = malloc( sizeof ( struct node ));

new_node -> data = num;

new_node -> next = *head;

*head = new_node;

}

// displaying linked lists from head node to tail node

void Display (struct node *node_ptr)

{

while (node_ptr != NULL)

{

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

node_ptr = node_ptr -> next;

if (node_ptr != NULL )

{

printf ("-->");

}

}

}

// comparing data of list1, list2 by linearly traversing from head to tail

int compare (struct node *head, int num)

{

while (head != NULL)

{

if (head -> data == num)

{

return 1;

}

head = head -> next;

}

return 0;

}

// function to get a union of two linked lists

struct node *get_union (struct node *List_one, struct node *List_two)

{

Union_list = NULL;

struct node *temp = List_one;

while (temp != NULL)

{

insert (&Union_list, temp -> data);

temp = temp -> next;

}

// add those nodes of list2 which is not present in list1 by comparing list1 and list2

while (List_two != NULL)

{

if (!compare (List_one, List_two -> data))

{

insert (&Union_list, List_two -> data);

}

List_two = List_two -> next;

}

return Union_list;

}

// function to get the intersection of two linked lists

struct node *get_intersection (struct node *List_one, struct *List_two)

{

Intersection_list = NULL;

while (List_one != NULL)

{

if (compare (List_two, List_one -> data))

{

insert (&Intersection_list, List_one -> data);

}

List_one = List_one -> next;

}

return Intersection_list;

}

// main function

int main()

{

int i, List_one_count, List_two_count, temp;

// creating linked list1

printf("Enter number of nodes in linked list1 : ");

fflush(stdout)

scanf("%d", &List_one_count);

printf("Enter values for %d nodes : ", List_one_count);

fflush(stdout)

for (i = 0; i < List_one_count; i++)

{

scanf("%d", &temp);

insert (&List_one, temp);

}

Display (List_one);

// creating linked list2

printf("Enter number of nodes in linked list2 : ");

fflush(stdout)

scanf("%d", &List_two_count);

printf("Enter values for %d nodes : ", List_two_count);

fflush(stdout)

for (i = 0; i < List_two_count; i++)

{

scanf("%d", &temp);

insert (&List_two, temp);

}

Display (List_two);

// displaying union list

get_union (List_one, List_two);

printf("\n\nUnion of two linked lists is : \n");

Display (Union_list);

// displaying intersection list

get_intersection (List_one, List_two);

printf("\n\nIntersection of two linked lists is : \n");

Display (Intersection_list);

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.