Challenge

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

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

ans)

Overview of the Project: (5 to 6 lines)

In this project,  interfaces for mathematical Set Theory are implemented using a C programming language. Users 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.

Example:

A={1,2,3,4} and b={3,4,5} then A Union B = {1,2,3,4,5} and A intersection B ={3,4}.

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 the 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
#include//this header file includes malloc size
typedef struct Node
{int data;
struct Node *next;
}node;
static node *head1,*head2,*head_union,*head_intersection,*newnode,*temp,*result_union,
*result_intersection;
//now i declaring different function with different arguments
node *insert_Node(node *head,int n);
node *get_union(node *head_list1,node *head_list2);
node *get_intersection(node *head_list1,node *head_list2);
void printing_list(node *head);
int compare(node *head,int data);
node *insert_Node(node *head,int n)
{
newnode = (node*) malloc(sizeof(node));//allocating the memory to the nodes
newnode->data = n;//assigning the value to node
newnode->next = head;//address of the previous address is transfered to the new node
head = newnode;//transferring the new address to the head for linking new node
return head;
}
node *get_union(node *head_list1,node *head_list2)
{
while(head_list1 != 0)
{
head_union = insert_Node(head_union,head_list1->data);//here we are inserting the data of
//list 1 into the union function
head_list1 = head_list1->next;//we traverse the node using the address in next.
}
while(head_list2 != 0)
{
if(!(compare(head_union,head_list2->data)))
{
head_union = insert_Node(head_union,head_list2->data);//here we are inserting
//the data of list 2 into the union function
}
head_list2 = head_list2->next;//we traverse the node using the address in next.
}
return head_union;
}
node *get_intersection(node *head_list1,node *head_list2)
{
while (head_list2 != 0)
{
if (compare(head_list1, head_list2->data))//we are comparing the both
//list for intersection
{
head_intersection = insert_Node(head_intersection,head_list2->data);
//transferring the that value to the intersection
}
head_list2 = head_list2->next;
}
return head_intersection;
}
void printing_list(node *head)
{
if(head != 0)
{
temp = head;
while(temp!=0)
{
printf("%d ", temp->data);// prints the data of current node
temp = temp->next;
}
temp = newnode;
}
else
{
printf(" It is empty set.");
}
}
int compare(node *head,int data)
{
while (head != NULL)
{
if (head->data == data)//comparing the data of list 2 with list 1
return 1;
head = head->next;
}
return 0;
}
int main()
{
int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,
second_list_items = 0;
int i;
printf("Enter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("\nEnter %d the elements:",first_list_nodes);//getting the number
//of element in the lists
for( i = 0;i < first_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&first_list_items);//getting the data in run time
head1 = insert_Node(head1,first_list_items);//inserting the data in list
}
printing_list(head1);//printing the head1 list
printf("\nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("\nEnter %d the elements:",second_list_nodes);
for(i = 0;i < second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items);//getting the data in run time
head2 = insert_Node(head2,second_list_items);
}
printing_list(head2);//displaying the head2 list
result_union = get_union(head1,head2);//transferring the result of
//union function to the result_union
printf("\nHere is the result of union: ");
printing_list(result_union);//transferring the values as arguments to printing function
result_intersection = get_intersection(head1,head2);//transferring the result of
//intersection function to the result intersection
printf("\nHere is the result of intersection: ");//transferring the result of
//intersection function to the res intersection
printing_list(result_intersection);//transferring the values as arguments to display function
}

 result:

 
 

Enter the number of Nodes in first List:4

Enter 4 the elements:1 2 5 6
6 5 2 1
Enter the number of Nodes in second List:4

Enter 4 the elements:9 8 1 5
5 1 8 9
Here is the result of union: 9 8 1 2 5 6
Here is the result of intersection: 1 5

 
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.