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 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.

OFFLINE

#include <stdio.h> // preprocessor
#include <stdlib.h> //standard library

//Define the nodes

struct node
{
int data;
struct node *next;//self referential node
};
//Define the function
//A utility function to insert a node at the beginning of a linked list
void push(struct node**head_ref,int key);
int isPresent(struct node *head,int data);
//Function to get union of two linked lists head1 and head2
struct node *getunion(struct node *head1,struct node *head2)
{
struct node *result=NULL;
struct node *x1=head1,*x2=head2;
// Insert all elements of
//first_set_items to the result list
while (x1!=NULL)
{
push(&result,x1->data);
x1=x1->next;
}
// Insert those elements of second_set_items
// which are not present in result second_set_items
while (x2!=NULL)
{
if(!isPresent(result,x2->data))
{
push(&result,x2->data);
}
x2=x2->next;
}
return result;

}
struct node* getintersec(struct node* head1,
struct node* head2)
{
struct node* result = NULL;
struct node* t1 = head1;
// Traverse second_set_items and search each element of it in
// second_set_items If the element is present in list 2, then
// insert the element to result
while (t1 != NULL) {
if (isPresent(head2, t1->data))
push(&result, t1->data);
t1 = t1->next;
}

return result;
}
void push(struct node**head_ref,int key)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
//put in the data
newnode->data=key;
//link the old list off the new node
newnode->next=(*head_ref);
//move the head to point to the new node
(*head_ref)=newnode;
}
//A utility function to print a linked list
void display(struct node*head)
{
struct node *k;

k=head;
while(k!=NULL)
{
printf(" %d ",k->data);
k=k->next;
}
}
//A utility function that returns true if data is present in linked list else return false
int isPresent(struct node* head, int data)
{
struct node* t = head;
while (t != NULL) {
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}

int main()
{
//Start with the empty list
struct node *head1=NULL;
struct node *head2=NULL;
struct node *reset_union=NULL;
struct node *reset_intersection=NULL;
int first_set_nodes = 0, first_set_items = 0;
int second_set_nodes = 0, second_set_items = 0;

printf("\nEnter the number of Elements in the first Set: ");
fflush(stdout);
scanf("%d", &first_set_nodes);

printf("Enter All %d elements: \n", first_set_nodes);
for(int i = 0; i < first_set_nodes; i++ )
{
fflush(stdout);
scanf("%d",&first_set_items);
push(&head1,first_set_items);
}
printf("\n list 1 =");
display(head1);


printf("\n Enter the number of Elements in the second Set: ");
fflush(stdout);
scanf("%d", &second_set_nodes);

printf("Enter All %d elements: \n", second_set_nodes);
for(int i = 0; i < second_set_nodes; i++ )
{
fflush(stdout);
scanf("%d",&second_set_items);
push(&head2,second_set_items);
}
printf("\n list 2 = ");
display(head2);


reset_union = getunion(head1, head2);
printf("\n The union of sets : ");
display(reset_union);
printf("\n");
reset_intersection = getintersec(head1, head2);
printf("The intersection of sets : ");
display(reset_intersection);

}

OUTPUT :


Enter the number of Elements in the first Set: 5
Enter All 5 elements:
1
2
3
4
5

list 1 = 5 4 3 2 1
Enter the number of Elements in the second Set: 5
Enter All 5 elements:
3
4
5
6
7

list 2 = 7 6 5 4 3
The union of sets : 6 7 1 2 3 4 5
The intersection of sets : 3 4 5
Process returned 0 (0x0) execution time : 10.975 s
Press any key to continue.

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.