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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;// Link list node
};
void push(struct Node** link_ref, int new_data);//function declaration
int isPresent(struct Node* link, int data);

struct Node* getunion(struct Node* link1, struct Node* link2)// Function to get getunion of two linked lists link1 and link2
{
struct Node* result = NULL;
struct Node *t1 = link1, *t2 = link2;
while (t1 != NULL) // Insert all elements of list1 to the result list
{
push(&result, t1->data);
t1 = t1->next;
}
while (t2 != NULL)// Insert those elements of list2 which are not present in result list
{
if (!isPresent(result, t2->data))
{
push(&result, t2->data);
}
t2 = t2->next;
}
return result;
}
struct Node* getIntersection(struct Node* link1, struct Node* link2)// Function to get intersection of two linked lists link1 and link2
{
struct Node* result = NULL;
struct Node* t1 = link1;
while (t1 != NULL)// Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result
{
if (isPresent(link2, t1->data))
{
push(&result, t1->data);
}
t1 = t1->next;
}
return result;
}

void push(struct Node** link_ref, int new_data)// A utility function to insert a node at the beginning of a linked list
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // allocate node
new_node->data = new_data; // put in the data
new_node->next = (*link_ref);// link the old list off the new node
(*link_ref) = new_node;// move the link to point to the new node
}

void printList(struct Node* node)// function to print a linked list
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}

int isPresent(struct Node* link, int data)// function that returns true if data is present in linked list else return false
{
struct Node* t = link;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}

int main()// Driver program to test above function
{
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d", &n, &m);
struct Node* link1 = NULL;
struct Node* link2 = NULL;
struct Node* inter = NULL;
struct Node* uni = NULL;// Start structure pointer with the empty list

for(int i=0;i<n;i++) //for loop for link list1
{
printf("Enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&link1, temp);
}

for(int i=0;i<m;i++)//for loop for link list2
{
printf("Enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&link2, temp);

}

inter = getIntersection(link1, link2); //call the function of intersection
uni = getunion(link1, link2);//call the function of getunion
printf("\n First list is ");
printList(link1);//function calling with structure pointer as argument
printf("\n Second list is ");
printList(link2);
printf("\n Intersection list is ");
printList(inter);
printf("\n getunion list is ");
printList(uni);
return 0;
}

Output:

enter the value of number of value in link list(both):-3
3
Enter the value in link list1 0:-1
Enter the value in link list1 1:-2
Enter the value in link list1 2:-2
Enter the value in link list2 0:-3
Enter the value in link list2 1:-2
Enter the value in link list2 2:-1

First list is 2 2 1
Second list is 1 2 3
Intersection list is 1 2 2
getunion list is 3 1 2 2

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.