Challenge

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

Aim of Project:

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

Description of Theory:

Linked list

The components of a linked list, a linear data structure, are linked together using pointers rather than being stored in a single continuous location. Each node in a connected list of nodes holds data as well as the address of the node after it.

Node structure: A node in a linked list normally has two parts:

Information: It contains the node's actual value or data.

Next Pointer: It keeps a reference to the next node in the chain's memory address.

Head and Tail: The head node, which points to the list's first node, is used to access the linked list. The list ends when the last node in the list points to NULL or nullptr. The tail node is this node's official name.

There are mainly three types of linked lists:

  1. Single-linked list
  2. Double linked list
  3. Circular linked list

 user define Data type:

"The user-defined data types in C" is a set of data whose values all have predetermined meanings. A preset value that aids in declaring, accessing, and storing data is known as a data type in C. The primary or built-in data types in C can be used to produce the user-defined data types.

Integer, char, float, and other primitive data types are known to exist in C. The foundational elements for user-defined data types in C are these primitive data types. The necessity, definition, and numerous user-defined data types in C will all be covered in this article.

there are four types of user define data types.

  • Structures
  • Union
  • Typedef
  • Enum

Mathematical Theory of Set:

In the area of mathematical logic known as set theory, we study sets and their characteristics. A set is a grouping or collection of objects. These things are frequently referred to as elements or set members.

Types of Sets

The sets are further categorised into different types, based on elements or types of elements. These different types of sets in basic set theory are:

  • Finite set: The number of elements is finite
  • Infinite set: The number of elements are infinite
  • Empty set: there is no element present in set
  • Singleton set: one only element present in set
  • Equal set: if same elements in Two sets are equal
  • Equivalent set: Two sets are equivalent if they have same number of elements
  • Power set: A set of every possible subset.
  • Universal set: Any set that contains all the sets under consideration.
  • Subset: When all the elements of set A belong to set B, then A is subset of B

        In this project we have to create the two sorted linked list. first we have declared the user define datatype struct. For that list we develop the c logic to take as number of node in list and data/elements in list as input. In this we check that the list is empty or nor. If it is empty then program will display the message. Same way we have take the input for second list. We have developed the logic for create the new node and insert this node into the list node. for that we check the condition that if list does not have any then head will the new node and the value of it will be same for head. If the list is not contain null value then it check the condition and insert the in list. Next in the C program we have to perform the mathematical set operation of union and intersection. Here we consider one sorted list as list1 (L1) and second sorted list as list1 (L2). In the case of union this operation compare two sets of list and gives the output list in which unique elements are present. In intersection operation of the mathematical set. In this operation it compare the two set and give the output list as common elements of two lists. The c logic for this mathematical set operation is developed in the c program.

Step for login implementation:

             Step 1 - Initialize the lists as NULL

              Step 2 - Take user input for number of nodes of both list

                        and create new nodes and assign user defined values to them

              Step 3 - Display the lists

              Step 4 - Perform union and display the union list

              Step 5 - Perform intersection and display the list

C Program for the project:

//Created on: 04/07/2023
 //Author: Arvind Hirole
 
 
#include    // library for C language
 
#include   // Library for malloc function
 
// Creating usert define datatype Node
struct Node
{
int value;
struct Node *next;// pointer pointing towards next node
};
 
 
struct Node *list1, *list2, *list_union, *list_intersection;// Initializing different list for program
void display_list(struct Node *p) // Function for display the list
{
// Check list status and print the list
if (p == NULL)
{
printf("\n the List is empty!!!");
}
else
{
while(p != NULL)
{
printf("%d ", p->value);
p = p->next;
}
}
}
 
 
struct Node *newNode(int value)   // create New Node 
{
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;
}
 
// insert the node in the exiting nodes
else
{
struct Node *current = *head;
while(current->next != NULL && current->next->value < newNode->value)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
 
 
int compare(struct Node *list, int data)  // Function to Compare two nodes
{
while(list != NULL)
{
 
if(list->value == data) //compra the values of nodes
{
return 1;
}
list = list->next;     //if node not match then go to next
}
 
return 0;
}
 
struct Node *get_union(struct Node *L1, struct Node *L2)
{
list_union = NULL;  //union list initialization as NULL
 
struct Node *temp = L1;
 
while(temp != NULL)  // Copying list 1 data to union list
{
insert_node(&list_union, newNode(temp->value));
temp = temp->next;
}
 
while(L2 != NULL)  // Copyunique data elements of list 2 in union list
{
 
if(!compare(L1, L2->value))  // Comparision list 1 and list 2 elements
{
 
insert_node(&list_union, newNode(L2->value)); // Copy unique elements to union list
}
 
L2 = L2->next;  // If matched,goto next adress
}
return list_union;
}
 
struct Node *get_intersection(struct Node *L1, struct Node *L2)
{
list_intersection = NULL;  // Initialize intersection list as NULL
 
while(L1 != NULL)  // list 1 and list 2 same elements Comparision
{
 
if(compare(L2,L1->value))
{
 
insert_node(&list_intersection,newNode(L1->value)); // matched elements copy to union 
}
 
L1 = L1->next;  // If not matched, goto next node address
}
 
// returning Intersection list
return list_intersection;
}
 
int main()  // Logic for main function
{
 
list1 = NULL;  //list 1 and list 2 initialization as NULL
list2 = NULL;
 
int list1_nodes, list2_nodes;  // variables declaration
int i, data;
 
printf("\nEnter number of nodes in list 1: "); 
fflush(stdout);
scanf("%d",&list1_nodes);
 
printf("\nEnter %d elements of list 1: ",list1_nodes);  //list 1 element form user
for(i = 0; i < list1_nodes; i++)
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list1,newNode(data));
}
 
printf("\nEnter number of nodes in list 2: ");  //list 2 nodes input from user
fflush(stdout);
scanf("%d",&list2_nodes);
 
printf("\nEnter %d elements of list 2: ",list2_nodes); //list 2 elements from user
for(i = 0; i < list2_nodes; i++)
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list2,newNode(data));
}
 
printf("\nList 1 is "); // list 1 display
display_list(list1);
 
printf("\nList 2 is "); // list 2 display
display_list(list2);
 
get_union(list1,list2); // Union operation of two list
 
printf("\nUnion of both list is ");  // Display Union output
display_list(list_union);
 
get_intersection(list1,list2);  // Intersection operation of two list
 
printf("\nIntersection of both list is ");  // Display Intersection of both list
display_list(list_intersection);
 
return 0;
}
 

Outputs:

1)

Enter number of nodes in list 1: 5

Enter 5 elements of list 1: 1 2 3 4 5

Enter number of nodes in list 2: 5

Enter 5 elements of list 2: 3 4 5 8 9

List 1 is 1 2 3 4 5

List 2 is 3 4 5 8 9

Union of both list is 1 2 3 4 5 8 9

Intersection of both list is 3 4 5

2)

Enter number of nodes in list 1: 6

Enter 6 elements of list 1: 10 12 15 75 86 45

Enter number of nodes in list 2: 6

Enter 6 elements of list 2: 15 45 42 17 63 91

List 1 is 10 12 15 45 75 86

List 2 is 15 17 42 45 63 91

Union of both list is 10 12 15 17 42 45 63 75 86 91

Intersection of both list is 15 45

Screen short of program and output:

Result:

In this way, we have inserted the new node into the sorted linked list which takes as input from the user and perform the mathematical union and insertion operation.

 
 
 
 
 
 
 
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.