Challenge

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

#include <stdio.h>

#include<stdlib.h>

//define a structure link list

struct link

{

int data; //store the value

struct link* next;// next element in the list.

};

void insert(struct link** head, int value)

{

// allocates memory for a new element

struct link*local=(struct link*)malloc(sizeof (struct link));

local-> data =value; //upadte the value of data

local-> next =*head;// new element to pointer

*head=local; //privision data

}

//display the elements of a linked list

void disp(struct link* head)

{

while (head!= NULL)

{

printf("%d\n",head->data);

head=head->next;

}

}

//The compare function checks

int compare(struct link*list,int data)

{

while (list!=0)

{

if (list -> data==data)

{

return 1;// if data existed on list return 1;

}

list=list->next;

}

return 0; // otherwise return to zero;

}

//function computes the union

struct link* get_union(struct link* list1,struct link* list2)

{

struct link *list_union=NULL;//initializes an empty linked list list_union.

//struct link *temp=list1;

while (list1!=0)

{

insert(&list_union,list1->data);//it iterates through list2, inserting element list_union of list1

list1=list1->next;

}

while (list2 !=0)

{

//it iterates through list2, inserting elements into list_union only if they are not already in list1

if(!compare (list1,list2->data))

{

insert(&list_union,list2->data);

}

list2=list2->next;

}

return list_union;

}

//function computes the intersection

struct link *get_intersection(struct link *list1,struct link*list2)

{

struct link* list_intersection=0;//define empty linklist

while (list1 !=0)

{

//It iterates through list1, inserting elements into list_intersection only if they exist in list2

if (compare (list2,list1->data))//compare list 1 and list2

{

insert(&list_intersection,list1->data);

}

list1=list1->next;

}

return list_intersection;

}

int main()

{

int n1,n2,d1,d2;

//define pointer is all zero

struct link * list1=0;

struct link * list2=0;

struct link* union1=0;

struct link* intersection =0;

//data of list 1

printf("Enter the how many number in list1 element :\n");

scanf("%d",&n1);

printf("Enter list1 element :\n");

for (int i=0;i<n1;i++)

{

scanf("%d",&d1);

insert(&list1,d1); // call the insert function

}

printf("Enter element \n");

disp(list1); //display the list1

//data of list 2

printf("Enter the how many number in list2 element :\n");

scanf("%d",&n2);

printf("Enter list2 element :\n");

for (int i=0;i<n2;i++)

{

scanf("%d",&d2);

insert(&list2,d2); // call the insert function

}

printf("Enter element \n");

disp(list2);//display the list2

//calculate the union of list1 and list2 using the get_union function

union1= get_union(list1,list2);

printf("Enter union element \n");

disp(union1);//display the union list

//calculate the intersection of list1 and list2 using the get_intersection function

intersection=get_intersection (list1,list2);

printf("Enter intersection element \n");

disp(intersection);//display the insersection list

return 0;

}

o/p:-Enter the how many number in list1 element :

3

Enter list1 element :

1

3

5

Enter element

5

3

1

Enter the how many number in list2 element :

4

Enter list2 element :

2

4

6

3

Enter element

3

6

4

2

Enter union element

2

4

6

3

1

3

5

Enter intersection element

3

...Program finished with exit code 0

Press ENTER to exit console.

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

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.