Challenge

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

#include//preprocessor file inclustion macro with user defined and Standard input and output
#include//preprocessorfile inclustion macro with user defined and string for character

struct Node //initilizes the structure data type
{
int data; //declared the integer data type
struct Node* next; // a structure node and stores the address of next variable
};

void push(struct Node** head_ref, int new_data); // the head of a list and an int, inserts a new node on the front of the list.
int isPresent(struct Node* head, int data); //initlizes the data in pointer of user data

struct Node* getUnion(struct Node* head1, struct Node* head2) // get union of two linked lists head1 and head2
{
struct Node* result = NULL; //creating a linked list
struct Node *n1 = head1, *n2 = head2; // Traverse both lists and store the element in the resu1tant list.
// Insert all elements of list1 to the result list
while (n1 != NULL) //initilizes he while loop if list1 is not equal to NULL
{
push(&result, n1->data); //push the data into list 1 and store in result
n1 = n1->next; // data is returned to store and next user input data in list 1
}

// Insert those elements of list2 which are not present in result list
while (n2 != NULL) //initilizes the while loop list2 not present in result
{
if (!isPresent(result, n2->data)) //initilizes the if loop list2 not present in result
{
push(&result, n2->data); //push the data inti list2
}
n2 = n2->next; // data is returned to store and next user input data in list2
}
return result; //main function returning to the result
}

// get intersection of two linked lists head1 and head2
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node* n1 = head1;
// Traverse list1 and search each element of it in list2.
//If the element is present in list 2, then insert the element to result
while (n1 != NULL) //initilizes the while loop list is not equal to null
{
if (isPresent(head2, n1->data)) //if loop data transverse to list 1 into head 2 location
{
push(&result, n1->data); //push the result of data into list1 into result
}
n1 = n1->next; // data is returned to store and next user input data in list 1
}
return result; //main function returning to the result
}
void push(struct Node** head_ref, int new_data)// insert a node at the beginning of a linked list
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));// allocate node and its size of node
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}


void printList(struct Node* node) // function to print a linked list
{
while (node != NULL) //initilizes the node data not equal to NULL
{
printf("%d ", node->data); //print the node data
node = node->next; //store the next input data from user
}
}
// function that returns true if data is present in linked list else return false
int isPresent(struct Node* head, int data)
{
struct Node* n = head; //node of pointer location to head of data
while (n != NULL) //initilizes the while loop transeverse list not equal to null
{
if (n->data == data) //initilizes data transper into transverse to data
{
return 1; //main function successfully executed by aruguments of function by returning 1
}
n = n->next; //transverse list data stores next input from user
}
return 0; //main function successfully executed by returning by 0
}


int main() // Driver program to test above function using main function
{
int i,p,q,temp; //declared the variables n,m and temp

printf("enter the value of number of value in link list(both): ");
fflush(stdout);
scanf("%d %d", &p, &q);
// structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL; NULL;
for( i=0; i{
printf("Enter the value in link list1 %d: ",i); //print enter values in list 1
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp); //push the data`from head 1 into temporary storage
}
//for loop for link list2
for( i=0; i{
printf("Enter the value in link list2 %d: ",i); //print the enter values in list 1
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp); //push the data`from head 2 into temporary storage
}
//function calling with structure pointer as argument
intersecn = getIntersection(head1, head2); //call the function of intersection
unin = getUnion(head1, head2); //call the function of union
printf("\n First list is ");
fflush(stdout);
printList(head1);
printf("\n Second list is ");
fflush(stdout);
printList(head2);
printf("\n Intersection list is ");
fflush(stdout);
printList(intersecn);
printf("\n Union list is ");
fflush(stdout);
printList(unin);
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.