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
Overview of the Project: (5 to 6 lines)
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
Goals (2 to 3 points)
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key Highlights of the Project: (4 to 5 point)
- Creating two list of data using linked list method.
- Insert the node to 1st and 2nd list accordingly.
- Intersection of data of two lists.
- Getting Union of two list.
Deliverables of this project: (4 to 5 points)
- Intersection of data of two list.
- Union of data of two list
Expected Outcomes: (4 to 5 points)
- Student will get the Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.
Code
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
void display1 (struct node *start1); //display the data of start 1 lined list
struct node *create1 (struct node *start1);
void display2 (struct node *start2); //display the data of start 2 lined list
struct node *create2 (struct node *start2);
struct node *intersect (struct node *start1, struct node *start2); //prototype function of intersection of two lnked
struct node *merge (struct node *start1, struct node *start2); //77prototype function of merging of two lnked
void Union (struct node *start1, struct node *start2); //union of two linked list
int
main () //main function
{
struct node *start1, *start2;
start1 = NULL;
start2 = NULL;
//fuction calling//
start1 = create1 (start1);
display1 (start1);
start2 = create2 (start2);
display2 (start2);
intersect (start1, start2);
merge (start1, start2);
printf ("after merging\n");
display1 (start1);
printf ("\nunion\n");
Union (start1, start2);
}
struct node *
create1 (struct node *start1)
{
struct node *temp1, *nn1;
int n1, i;
printf ("enter length of linked list1");
fflush (stdout);
scanf ("%d", &n1);
for (i = 0; i < n1; i++)
{
//creating memory for linked list 1
nn1 = (struct node *) malloc (sizeof (struct node));
fflush (stdout);
scanf ("%d", &nn1->data);
nn1->next = NULL;
if (start1 == NULL)
{
temp1 = start1 = nn1;
}
else
{
temp1->next = nn1;
temp1 = temp1->next;
}
}
return (start1);
}
void
display1 (struct node *start2)
{
struct node *temp2;
temp2 = start2;
while (temp2 != NULL)
{
printf ("%d->", temp2->data);
temp2 = temp2->next;
}
}
struct node *
create2 (struct node *start2)
{
struct node *temp2, *nn2;
int n2, j;
printf ("enter length of linked list2");
fflush (stdout);
scanf ("%d", &n2);
for (j = 0; j < n2; j++)
{
//creating memory for linked list 2
nn2 = (struct node *) malloc (sizeof (struct node));
fflush (stdout);
scanf ("%d", &nn2->data);
nn2->next = NULL;
if (start2 == NULL)
{
temp2 = start2 = nn2;
}
else
{
temp2->next = nn2;
temp2 = temp2->next;
}
}
return (start2);
}
void
display2 (struct node *start2)
{
struct node *temp2;
temp2 = start2;
while (temp2 != NULL)
{
printf ("%d->", temp2->data);
fflush (stdout);
temp2 = temp2->next;
}
}
//function definition of intersection of two linked list//
struct node *
intersect (struct node *start1, struct node *start2)
{
struct node *x, *y;
for (x = start1; x != NULL; x = x->next)
{
for (y = start2; y != NULL; y = y->next)
{
if (x->data == y->data)
{
printf ("intersect of 2 linked list");
fflush (stdout);
printf ("\n%d\n", x->data);
break;
}
}
}
return (start1);
}
//function definition of mergring two linked list//
struct node *
merge (struct node *start1, struct node *start2)
{
struct node *temp;
temp = start1;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = start2;
return (start1);
}
//function definition of sorting of two linked list//
struct node *
sort (struct node *start1, struct node *start2)
{
struct node *a, *b;
int temp;
for (a = start1; a != NULL; a = a->next)
{
for (b = a->next; b != NULL; b = b->next)
{
if (a->data > b->data)
{
temp = a->data;
a->data = b->data;
b->data = temp;
}
}
}
return (start1);
}
//function definition of union of two linked list//
void
Union (struct node *start1, struct node *start2)
{
struct node *i, *j;
i = start1;
j = start2;
for (i = start1; i != NULL; i = i->next)
{
for (j = i->next; j != NULL; j = j->next)
{
if (j->next == NULL)
{
printf ("%d,", j->data);
}
if (i->data == j->data)
{
break;
}
else
{
printf ("%d,", i->data);
break;
}
}
}
}
Output
4->5->2->enter length of linked list23
2
7
1
2->7->1->intersect of 2 linked list
2
after merging
4->5->2->2->7->1->
union
4,5,2,1,7,
