Challenge

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

// Library
#include 
#include 
#include 

//Global Variable Initialization
int k, m;

typedef struct node
{
int data;
struct node* link;
} Node;

// Function prototype
struct node* getUnion(struct node* head1, struct node* head2);
struct node* getIntersection(struct node* head1,struct node* head2);
bool isPresent(struct node* head, int data);
void one(int n, int s);
void print(Node* head);
void push(struct node** head_ref, int new_data);

Node* head1 = NULL;
Node* head2 = NULL;
Node* previous = NULL;
Node* inter = NULL;
Node* uni = NULL;

int main(void)
{
// First linked list
printf("\n To create and display Linked List n ");
printf("\n Input the number of nodes for first linked list: n - ");
scanf("%d", &k);
one(1, k);
print(head1);

//Second linked list
printf("\n Input the number of nodes for second linked list: n - ");
scanf("%d", &m);
one(2, m);
print(head2);

//Intersection
inter = getIntersection(head1, head2);
printf("n Intersection list is n");
print(inter);

//Union
uni = getUnion(head1, head2);
printf("n Union list is n");
print(uni);

return 0;
}

//Create the two list
void one(int n, int s)
{
if(n==1)
{
printf("Enter first linked list n");
int data = 0;
for(int i=0; idata = data; // Assign the data
ptr->link = NULL; // Assign null, replace by address after getting address

if(head1 == NULL) // Run only first time
{
head1 = ptr; // Equal head1 with ptr
}
else
{
previous->link = ptr; // Assign the current address with previous one
}

previous = ptr; //equal previous with Ptr to assign current address with previous
}
}

else if(n==2)
{
printf("\n  Enter second linked list n ");
int data = 0;
for(int i=0; idata = data;// Assign the data
ptr->link = NULL;// Assign null, replace by address after getting address

if(head2 == NULL) // Run only first time
{
head2 = ptr; // Equal head1 with ptr

}
else
{
previous->link = ptr; // Assign the current address with previous one
}

previous = ptr; //equal previous with Ptr to assign current address with previous
}
}

}

//Print the result
void print(Node* head) // Pass the address to print
{
printf("Result: ");
while(head!=NULL) // check if it null
{
printf("%d ", head->data); //print the data
head = head->link; // Increment the address
}
printf("n");
}

// Function to get union of two list
struct node* getUnion(struct node* head1,struct node* head2) // Pass head1 & head2 address
{
struct node* result = NULL; // Create the result node & assign it to NULL
struct node *t1 = head1, *t2 = head2; // Create two node & assign it to head 1&2

// Insert all elements of head1 to the head adress result
while (t1 != NULL) {
push(&result, t1->data); // push all the data with one head address
t1 = t1->link; // Increment the address
}


// Compare data with head1 & head2 and insert the data which is not present in head1
while (t2 != NULL) {
if (!isPresent(result, t2->data))
push(&result, t2->data); // push all the data with one head address
t2 = t2->link; // Increment the address
}

return result; // return the result
}

// Function to get intersection of two list
struct node* getIntersection(struct node* head1,struct node* head2)
{
struct node* result = NULL; // Create the result node & assign it to NULL
struct node* t1 = head1; // Create node & assign it to head 1

// Compare data with head1 & head2 and insert the data which is present in both list
while (t1 != NULL) {
if (isPresent(head2, t1->data))
push(&result, t1->data); // push all the data with one head address
t1 = t1->link; // Increment the address
}

return result;
}

// Function to insert a node
void push(struct node** head_ref, int new_data)
{
struct node* new_node = (struct node*)malloc(sizeof(struct node)); //Create the node & allocate the memory for each node

new_node->data = new_data; // put in the data

new_node->link = (*head_ref); // head_ref point to the pointer of new_node

(*head_ref) = new_node; //move the head to point to the new node
}

// Return true if both data are equal
bool isPresent(struct node* head, int data)
{
while (head != NULL) { // Check the memory allocation
if (head->data == data) // Check the condition
return 1; // Return true if equal
head = head->link; //Increment the address
}
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.