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

struct node {
int data;
struct node* next; //self referencial node
};

struct node* create(struct node** head, int n ) //we can use this create function to create list
{
*head = NULL; // empty list
int i, data;

printf("Enter the numbers:\n");
for (i = 0; i < n; i++) {
scanf("%d", &data);
//the following three lines to create new node
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
//the following steps to create link
if (*head == NULL) {
*head = temp;
} else {
struct node* temp1 = *head;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp1->next = temp;
}
}

return *head;
}

struct node* unionOfLists(struct node* head1, struct node* head2) // Function to perform the union of two linked list
{
struct node* unionHead = NULL;
struct node* unionTail = NULL;

// Copy elements of head1 to the union list
while (head1 != NULL) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = head1->data;
newNode->next = NULL;

if (unionHead == NULL) {
unionHead = newNode;
unionTail = newNode;
} else {
unionTail->next = newNode;
unionTail = newNode;
}

head1 = head1->next;
}

// Check for duplicates in head2 and add unique elements to the union list
while (head2 != NULL) {
int duplicate = 0;
struct node* temp = unionHead;
while (temp != NULL) {
if (temp->data == head2->data) {
duplicate = 1;
break;
}
temp = temp->next;
}

if (!duplicate) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = head2->data;
newNode->next = NULL;

unionTail->next = newNode;
unionTail = newNode;
}

head2 = head2->next;
}

return unionHead;
}
struct node* intersectionOfLists(struct node* head1, struct node* head2) // Function to perform the intersection of two linked list
{
struct node* intersectionHead = NULL;
struct node* intersectionTail = NULL;

while (head1 != NULL) {
int found = 0;
struct node* temp = head2;
while (temp != NULL) {
if (temp->data == head1->data) {
found = 1;
break;
}
temp = temp->next;
}

if (found) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = head1->data;
newNode->next = NULL;

if (intersectionHead == NULL) {
intersectionHead = newNode;
intersectionTail = newNode;
} else {
intersectionTail->next = newNode;
intersectionTail = newNode;
}
}

head1 = head1->next;
}

return intersectionHead;
}

void print(struct node* head) {
// printf("List is: ");
while (head != NULL) {
printf("%d\t", head->data);
head = head->next;
}
printf("\n");
}

int main() //main function
{
struct node* head1 = NULL; //initiating list 1
struct node* head2 = NULL; //initiating list 2
int n1, n2; //number of nodes
printf("How many numbers for list 1?\n");
scanf("%d", &n1);
head1 = create(&head1, n1);
printf("List1 is: ");
print(head1);
printf("How many numbers for list 2?\n");
scanf("%d", &n2);
head2 = create(&head2, n2);
printf("List2 is: ");
print(head2);
struct node* unionList = unionOfLists(head1, head2);
printf("Union of the two lists: ");
print(unionList);
struct node* intersectionList = intersectionOfLists(head1, head2);
printf("Intersection of the two lists: ");
print(intersectionList);
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.