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


// header files
#include<stdio.h>
#include<stdlib.h>

//
struct node
{
int data; // data of the node
struct node *nextptr; // address of the next node
};

int TEST; /* declaring a global variable to transfer the number between functions */

/*
* function for entering the data in the desired linklist
*/

void data_entry(struct node **head, int num)
{
struct node* new_node = (struct node*)malloc(sizeof (struct node));
new_node->data = num;
new_node->nextptr = *head;
*head = new_node;
}


/*
* function for searching the data in the linklist
*/
int search(struct node * head, int num)
{
while( head != NULL)
{
if(head->data == num)
{
TEST = head->data; // TEST global variable is used to transfer the num for comparison
return (1);

}
head = head->nextptr;
}
return (0);
}

/*
* function used to find the union of 2 given linklist
*/

struct node* find_unin(struct node* head1, struct node* head2)
{
struct node* result = NULL;
struct node *t1= head1, *t2=head2;
while( t1 != NULL)
{
data_entry(&result,t1->data);
t1 = t1->nextptr;
}
while( t2 != NULL)
{
search(&t1,t2->data);
if(TEST != t2->data)
{
data_entry(&result,t2->data);
}
t2 = t2->nextptr;
}
return result;
}

/*
* function used to find the intersection of 2 given linklist.
*/

struct node* find_intersec(struct node *head1, struct node *head2)
{
struct node* result = NULL;
struct node *t1= head1, *t2=head2;

while(t1 != NULL)
{
search(t2,t1->data);

if ( TEST == t1->data)
{
data_entry(&result,t1->data);
}
t1 = t1->nextptr;
}
return(result);

}

/*
* function used to print the vaiables.
*/

void display(struct node* print)
{
while(print != NULL)
{
printf("%d",print->data);
print = print->nextptr;
}
}

/*
* The main function
*/

int main()
{
int L1_count,L2_count,temp;
struct node* list1 = NULL; // declaring the node lists
struct node* list2 = NULL;
struct node* unin = NULL;
struct node* intersec = NULL;
printf("\n Enter the no of nodes at linklist 1 : ");
fflush(stdout);
scanf("%d",&L1_count);

printf("\n Enter the %d values of the linklist 1 : ",L1_count);
fflush(stdout);
for(int i=0; i < L1_count; i++)
{
scanf("%d",&temp);
data_entry(&list1,temp);
}
printf("\n Enter the no of nodes at linklist 2 : ");
fflush(stdout);
scanf("%d",&L2_count);

printf("\n Enter the %d values of the Linklist 2 : ",L2_count);
fflush(stdout);
for(int i=0; i < L2_count; i++)
{
scanf("%d",&temp);
data_entry(&list2,temp);
}
unin = find_unin(list1,list2);
intersec = find_intersec(list1,list2);

// printing out the datas
printf("\n linklist 1: ");
display(list1);
printf("\n linklist 2:");
display(list2);
printf("\n union in the given set is :");
display(unin);
printf("\n intersection of the given set is :");
display(intersec);
return(0);

}

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.