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 code

#include <stdio.h>
#include <stdlib.h>
// Link list node
struct Node {
int data;
struct Node* next;
};
//function declaration
void push(struct Node** head_ref, int new_data);
int isPresent(struct Node* head, int data);
// Function to get union of two linked lists list1 and list2
struct Node* getUnion(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
struct Node *p1 = list1, *p2 = list2;
// Insert all elements of list1 to the result list
while (p1 != NULL)
{
push(&result, p1->data);
p1 = p1->next;
}
// Insert those elements of list2 which are not present in result list
while (p2 != NULL)
{
if (!isPresent(result, p2->data))
{
push(&result, p2->data);
}
p2 = p2->next;
}
return result;
}
// Function to get intersection of two linked lists list1 and list2
struct Node* getintersection(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
struct Node* p1 = list1;
// search each element of it in list2. If the element is present in list 2, then insert the element to result
while (p1 != NULL)
{
if (isPresent(list2, p1->data))
{
push(&result, p1->data);
}
p1 = p1->next;
}
return result;
}
// A utility function to insert a node at the beginning of a linked list
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// put in the data
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to print a linked list
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
// function that returns true if data is present in linked list else return false
int isPresent(struct Node* list, int data)
{
struct Node* p = list;
while (p != NULL)
{
if (p->data == data)
{
return 1;
}
p = p->next;
}
return 0;
}
// Driver program to test above function
int main()
{
int x,y,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d",&x, &y);
// Starth structure pointer with the empty list
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link list1
for(int i=0;i<x;i++)
{
printf("Enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&list1, temp);

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

}
//call the function of intersection
intersecn = getintersection(list1, list2);
//call the function of union
unin = getUnion(list1, list2);
printf("\n First list is ");
//function calling with structure pointer as argument
printList(list1);
printf("\n Second list is ");
printList(list2);
printf("\n Intersection list is ");
printList(intersecn);
printf("\n Union list is ");
printList(unin);
return 0;
}

output(1)

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

First list is 4 3 2
Second list is 4 3 2 1
Intersection list is 2 3 4
Union list is 1 2 3 4

output(2)

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

First list is 4 3 2
Second list is 6 4 2
Intersection list is 2 4
Union list is 6 2 3 4

1 comment
  1. Rohit Kumarmishragood
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.