Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
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 the Project:
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists.
Tools used:
PC with STM32 IDE/ GDB Online debugger.
Algorithm/Procedure:
step1:Get the number of elements from user for list1
step2:Then get the elements for list1 from the user via console
step3:Get the number of elements from user for list2
step4:Then get the elements for list2 from the user via console
step5:Call create list function to create linked list1 and 2
step6:Call display list function to display list1 and list 2 respectively
step7:call intersection function to find the common elements from list1 and 2.
step8:call Union function to find the common elements from list1 and 2.
step9:Call display list function again to display the outputs of U and n ....respectively
Program:
/*
* project1.c
*
* Created on: 13-Sep-2022
* Author: Kavitha
*/
# include <stdio.h>
# include <stdlib.h>
//Definition of the node
struct node{
int data;
struct node*next;
};
//Creation of linked lists
struct node *createlist()
{
int n;
struct node*head = NULL, *p, *ptr;
int x,i;
printf(" Enter the Number of Elements to be added under list1: \n");
fflush(stdout);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the data: ");
fflush(stdout);
scanf("%d",&x);
p= (struct node*)malloc(sizeof(struct node));
p ->data = x;
p ->next = NULL;
if(head==NULL)
{
head=ptr=p;
}
else
{
ptr->next=p;
ptr=p;
}
}
return head;
}
//Displaying of list & results of union and intersection operations
void displaylist (struct node*list)
{
struct node*p;
p=list;
while(p!=0)
{
printf("%d \t",p->data);
p=p->next;
}
}
//Function for calculating intersection elements from list1 and list2
int intersection(struct node*list1,struct node*list2)
{
printf("\n Intersection list: \t");
fflush(stdout);
struct node*temp1=list1;
struct node*temp2=list2;
while(temp1)
{
while(temp2)
{
if(temp1->data == temp2->data) //comparing to find intersecting elements
{
printf("%d \t",temp1->data);
}
temp2=temp2->next;
}
temp1=temp1->next;
temp2=list2;
}
return 0;
}
//Function for calculating Union elements from list1 and list2
int Union(struct node*list1,struct node*list2)
{
printf("\n Union list: \t");
fflush(stdout);
struct node*temp1=list1;
struct node*temp2=list2;
displaylist(temp1);
while(temp2)
{
if(temp1->data != temp2->data) //comparing to finding union elements
{
printf("%d \t",temp2->data);
}
temp2 = temp2->next;
}
return 0;
}
//main program
int main()
{
struct node *list1,*list2;
list1=list2=NULL; // Initially assigning both lists as NULL elements
printf("list1: \n");
list1=createlist(); // calling a function for creation of 1st linked lists
printf("list2: \n");
list2=createlist(); // calling a function for creation of 2nd linked lists
printf("\n list1 is: ");
displaylist(list1); //List1 elements are displayed
printf("\n list2 is: ");
displaylist(list2); //List2 elements are displayed
intersection(list1,list2); // calls intersection function for finding the results between 2 lists
Union(list1,list2); //// calls union function for finding the results between 2 lists
}
Output/Results:

Conclusion:
Thus using multiple function calls 2 linked lists were created and their set operations like "union and intersection operations" were performed successfully and the results were shown using embedded c programming concepts with the help of STM32 IDE.