Challenge

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

Overview of the Project:

In this project,  interfaces for mathematical Set Theory are implemented using a C programming language. Users can give the input like two lists of data and this program will give  the output as the union and intersection of these two lists

Goals:

  • Implementation of interface for mathematical Set Theory using C programming.
  • Giving two lists of data as input to get the output as union & intersection for the given list of data.

Key Highlights of the Project: 

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

Expected Outcomes:

  • Getting the intersection and Union of data of two list
  • Comparing the elements of the two lists.

Program Code:

#include //We have to include stdio.h (standard input output library for C language)to use the print function
#include //standard library includes functions involving memory allocation, process control, conversions and others

struct node{ //creating a node with structures
struct node*NextAddress; //Initializing the next address variable for storing the address of the next node
int Value; // Initializing value variable for storing data
};

void sortlist(); //Initializing Void Sorted list function
void Display(); //Initializing Void Display function

struct node *CreateList(int n) //Initializing and Defining the create list variable for storing linked list value
{
struct node * Head_Node = NULL; //Initializing Head Node Which will point to the head node of the linked list
struct node * Current_Node = NULL; //Initializing Current Node which will point to the Current node of the linked list

int Value; //Initializing Value Variable for storing data entered by the user
int i; //Initializing i variable for (for loop)
for (i=1;i<=n;i++) //Starting of for loop, the loop will run until it reaches the last element
{
printf("Enter element %d: ",i); //Asking user to enter Element value
fflush(stdout); //carries output to console
scanf("%d",&Value); //Entered Value will be stored in the VALUE Variable

struct node * New_Node = (struct node *) malloc(sizeof(struct node)); //Dynamic memory allocation of New_Node
New_Node->Value = Value; //VALUE is stored in New Node
New_Node->NextAddress = NULL;
if(Head_Node == NULL)
{
Head_Node = Current_Node = New_Node; //if Head Node is NULL, store New Node value in Head Node
}
else
{
Current_Node->NextAddress = New_Node; //Next_Node will be stored in the next address of Current_node
Current_Node = Current_Node->NextAddress; //Moving to the Next Address of the Current Node
}
}
return Head_Node; //Returning all the values stored in the linked list from the start of head node
}

void sortlist() //Initializing and Defining the Sort list
{
struct node *Head_Node; //Initializing Head_Node
struct node *Current_Node; //Initializing Current_node,
struct node *Next_Node = NULL; //Initializing next_node
int temp; //creating a temporary variable
if(Head_Node == NULL)
{
printf("\nCreated List is Empty");
exit(0);
}
else
{
while(Current_Node != NULL)
{
Next_Node = Current_Node->NextAddress; //Next_Node will point next address of Current_node

while(Next_Node != NULL)
{
if(Current_Node->Value > Next_Node->Value)
{
temp = Current_Node->Value; //temporary variable will store current node value
Current_Node->Value = Next_Node->Value; //now the current node is empty, then we are transferring the next node value into the current node
Next_Node->Value = temp; //now next node will store a temporary value and two values will be swapped
}
Next_Node = Next_Node->NextAddress; //Moved to the Next Address of Next Node
}
Current_Node = Current_Node->NextAddress; //Moved to the Next Address of Current Node
}
}
}

struct node * Union(struct node * List1, struct node * List2) //Initializing and Defining Union
{
struct node * Head_Node = NULL; //Initializing and defining Head_Node
struct node * Current_Node = NULL; //Initializing and defining Current_Node
while(List1!=NULL && List2!=NULL)
{
struct node * New_Node = (struct node *) malloc(sizeof(struct node)); //Dynamic memory allocation of New_Node
New_Node->NextAddress = NULL;
if(List1->Value < List2->Value)
{
New_Node->Value = List1->Value; //if(L1->V < L2->V),L1->V is stored in new node
List1 = List1->NextAddress; //Moved to the Next Address of L1
}
else if(List1->Value > List2->Value)
{
New_Node->Value = List2->Value; //if(L1->V > L2->V),L2->V is stored in new node
List2 = List2->NextAddress; //Moved to the Next Address of L1
}
else
{
int Data = List1->Value; //L1 value is stored in Variable Data
New_Node->Value = Data; //next node value is stored is also stored in the data variable

while(List1 != NULL && List2 != NULL && List1->Value == Data && List2->Value == Data)
{
List1 = List1->NextAddress; //Moved to the Next Address of L1
List2 = List2->NextAddress; //Moved to the Next Address of L2
}
}
if(Head_Node == NULL)
{
Head_Node = Current_Node = New_Node; //if head = NULL allot current node as head
}
else
{
Current_Node->NextAddress = New_Node; //New node is stored in the Current node next address
Current_Node = Current_Node->NextAddress; //Moved to the Next Address of Current Node
}
}
while(List1 != NULL)
{
Current_Node->NextAddress = (struct node *) malloc(sizeof(struct node)); //Dynamic memory allocation of Address of Current Node
Current_Node = Current_Node->NextAddress; //Moved to the Next Address of Current node
Current_Node->Value = List1->Value; //L1 value is stored in Current Node
List1 = List1->NextAddress; //Moved to the Next Address of L1
}
while(List2 != NULL)
{
Current_Node->NextAddress = (struct node *) malloc(sizeof(struct node)); //Dynamic memory allocation of Address of Current Node
Current_Node = Current_Node->NextAddress; //Moved to the Next Address of Current node
Current_Node->Value = List2->Value; //L1 value is stored in Current Node
List2 = List2->NextAddress; //Moved to the Next Address of L2
}
Current_Node->NextAddress = NULL;
return Head_Node; //Return Union of Two Linked List
}

struct node * intersection(struct node * List1, struct node* List2) //Initializing and Defining Intersection
{
if(List1 == NULL || List2 == NULL)
{
return NULL;
}
struct node * Head_Node = NULL; //Initializing and defining Head_Node
struct node * Current_Node = NULL; //Initializing and defining Current_Node

while(List1 != NULL && List2 != NULL)
{
if(List1->Value < List2->Value)
{
List1 = List1->NextAddress; //Moved to the Next Address of L1
}
else if(List2->Value < List1->Value)
{
List2 = List2->NextAddress; //Moved to the Next Address of L2
}
else
{
int Data = List1->Value; //L1 value will be stored in data
struct node * New_Node = (struct node *) malloc(sizeof(struct node));//Dynamic memory allocation of New Node
New_Node->Value = Data; //Data Value is stored in New Node
New_Node->NextAddress = NULL;
if(Head_Node == NULL)
{
Current_Node = Head_Node = New_Node; //If Head Node is NULL store new node value in the current node
}
else
{
Current_Node->NextAddress = New_Node; //New Node value is stored in the current node address
Current_Node = Current_Node->NextAddress; //Moved to the Next Address of Current node
}
while(List1 != NULL && List2 !=NULL && List1->Value == Data && List2->Value == Data)

{
List1 = List1->NextAddress; //Moved to the Next Address of L1
List2 = List2->NextAddress; //Moved to the Next Address of L2
}
}
}
return Head_Node; //Return Intersection of Two Linked List
}

void Display(struct node * Head_Node) //Initializing Display function to display the Linked List
{
if(Head_Node == NULL) //Checking if Head is NULL or NOT
{
printf("Empty List\n"); //If NULL print this function and exit
return;
}
while(Head_Node != NULL) //While will end when last node = NULL
{
printf("%d ",Head_Node->Value); //printing All values in a linked list from head to last node
Head_Node = Head_Node->NextAddress; //Head_Node will point next address of Head_Node
}
}

int main()
{
struct node *Head_Node=NULL; //Initializing Head_Node and Defining it as NULL
struct node *List1 = NULL; //Initializing L1 and Defining it as NULL
struct node *List2 = NULL; //Initializing L2 and Defining it as NULL
struct node *List3 = NULL; //Initializing L3 and Defining it as NULL
struct node *List4 = NULL; //Initializing L4 and Defining it as NULL


int n; //Initializing Variable n

printf("\nEnter Total No of Elements in list1: \n"); //Total no of elements in the linked L1
fflush(stdout); //carries output to console
scanf("%d",&n); //Stores value of total elements in variable n
List1 = CreateList(n); //L1 stores the created list.
sortlist(Head_Node); //Sort the created list
Display(List1); //Displays the Sorted list
printf("\n^Above is the Sorted Linked List1\n"); //printing statement


printf("\nEnter Total No of Elements in List2: "); //Total no of elements in the linked L2
fflush(stdout); //carries output to console
scanf("%d",&n); //Stores value of total elements in variable n
List2 = CreateList(n); //L2 stores the created list.
sortlist(Head_Node); //Sort the created list
Display(List2); //Displays the Sorted list
printf("\n^Above is the Sorted List2\n");

printf("\nUnion of Two Linked List: "); //printing statement
List3 = Union(List1, List2); //L3 stores Union of L1 and L2
Display(List3); //Displays the sorted list

printf("\nIntersection of Two Linked List: "); //printing statement
List4 = intersection(List1, List2); //L4 stores intersection of L1 and L2
Display(List4); //Displays the Intersection list

return 0; //returns 0 value after execution of the main function
}

---------------------------------------------------------------------------------------------------------------------

Create List Function:

--------------------------------------------------------------------------------------------------------------------------

Sorted List Function:

 

--------------------------------------------------------------------------------------------------------------------------

Union Function:

---------------------------------------------------------------------------------------------------------------------------

Intersection  Function:

----------------------------------------------------------------------------------------------------------------------------

Display Function:

-----------------------------------------------------------------------------------------------------------------------------

Main Function:

---------------------------------------------------------------------------------------------------------------------------

Console Output:

Example 1:

Example 2:

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.