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: (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.

Project:

// Mathematical project in c-programing

#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>


//Linked list
struct node
{
int data;
struct node* next;
};

void push(struct node** head_ref,int new_data);
bool isPresent(struct node* head,int data);
// union of two linked list
struct node* getunion(struct node* head1,struct node* head2)
{

struct node* result =NULL;
struct node *t1 = head1, *t2 = head2;

while (t1 != NULL) // list one elements
{
push(&result, t1->data);
t1 = t1->next;
}
while (t2 != NULL) // list two
{
if(!isPresent(result,t2->data))
push(&result,t2->data);
t2=t2->next;
}
return result;
}
// function to get intersection
struct node* getintersection(struct node* head1,struct node* head2)
{
struct node* result = NULL;
struct node* t1 = head1;

while (t1 != NULL)
{
if(isPresent(head2, t1->data))
push(&result, t1->data);
t1 = t1->next;
}
return result;
}
// linked list insertion function
void push(struct node** head_ref,int new_data)
{
struct node* new_node = (struct node*)malloc(sizeof(struct node));

new_node->data = new_data; //put in the data
new_node->next = (*head_ref); //Make next of new node as head
(*head_ref) = new_node; //move the head to point to the new node
}
// Display linked list
void display(struct node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
//function that returns true if data is present in linked list else return false
bool isPresent(struct node* head,int data)
{
struct node* t = head;
while (t != NULL)
{
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}

// main function to run above program
int main()
{
int a,b,c,d,w,x,y,z;
struct node* head1=NULL;
struct node* head2=NULL;
struct node* intersection=NULL;
struct node* unin=NULL;

printf("Enter four elements for the first array:");
fflush(stdout);
scanf("\n %d",&a);
scanf("\n %d",&b);
scanf("\n %d",&c);
scanf("\n %d",&d);

printf("Enter four elements for the second array:");
fflush(stdout);
scanf("\n %d",&w);
scanf("\n %d",&x);
scanf("\n %d",&y);
scanf("\n %d",&z);

// first linked list with input data
push(&head1, a);
push(&head1, b);
push(&head1, c);
push(&head1, d);

// second linked list with input data
push(&head2, w);
push(&head2, x);
push(&head2, y);
push(&head2, z);

intersection = getintersection(head1, head2);
unin = getunion(head1, head2);

printf("\n First list is \n");
display(head1);

printf("\n Second list is \n");
display(head2);

printf("\n Intersection list is \n");
display(intersection);

printf("\n Union list is \n");
display(unin);

return 0;
}

output

Enter four elements for the first array:1 2 3 4
Enter four elements for the second array:2 3 4 5

First list is
4 3 2 1
Second list is
5 4 3 2
Intersection list is
2 3 4
Union list is
5 1 2 3 4

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.