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>//pre-processor

#include <stdlib.h>

//defining a node using structure
struct node
{
int value;

struct node *next;

};

struct node *head_1 = NULL;

struct node *head_2 = NULL;

struct node *union1 = NULL;

struct node *intersec1 = NULL;

//function for inserting a node at the beginning
void InsertNode(struct node **head, int num)
{
//creating a new node
struct node *new;

//allocating memory
new = malloc(sizeof(struct node));

//initializing data part of the node
new -> value = num;

//link part points to head node of linked list
new -> next = *head;

//assign new node as head node
*head = new;

}
//function for displaying the linked lists
void Display(struct node *p)
{
while (p != NULL)
{
printf("%d \n", p -> value);

p = p -> next;

}
}

//function to compare the node data with a value by linearly traversing across head to tail in a linked list
int compare(struct node *head, int num)
{
while (head != NULL)
{
if (head -> value == num)

return 1;

head = head -> next;
}
return 0;
}

//function to get the union of two linked lists
struct node *get_union(struct node *head_1, struct node *head_2)
{
union1 = NULL;

struct node *p1 = head_1;

//add all the nodes from linked list 1 to union1
while (p1 != NULL)
{
InsertNode(&union1, p1 -> value);

p1 = p1 -> next;
}

//add those values which are not present in union1 by comparing Linked list 1 and 2
while (head_2 != NULL)
{
if (!compare(head_1, head_2 -> value))
{
InsertNode(&union1, head_2 -> value);
}
head_2 = head_2 -> next;
}
return union1;
}
//function to get the intersection of two linked lists
struct node *get_intersection(struct node *head_1, struct node *head_2)
{
intersec1 = NULL;

//add those values present in both linked lists by comparing the values of linked list 1 and 2
while (head_1 != NULL)//
{
if (compare(head_2, head_1 -> value))
{
InsertNode(&intersec1, head_1 -> value);
}
head_1 = head_1 -> next;
}

return intersec1;
}

int main()
{
int i, n1, n2, temp;

//
printf("Enter the number of nodes in first linked list: ");

fflush(stdout);

scanf("%d", &n1);

printf("Enter the values for %d nodes: ", n1);

fflush(stdout);

for (i = 0;i < n1; i++)
{
scanf("%d", &temp);

InsertNode(&head_1, temp);
}
Display(head_1);

//creating second linked list
printf("Enter the number of nodes in second linked list: ");

fflush(stdout);

scanf("%d", &n2);

printf("Enter the values for %d nodes: ", n2);

fflush(stdout);

for (i = 0;i < n2; i++)
{
scanf("%d", &temp);

InsertNode(&head_2, temp);
}
Display(head_2);

D
get_union(head_1, head_2);

printf("Union of the two linked lists: \n");

Display(union1);

//creating intersection of both linked lists
get_intersection(head_1, head_2);

printf("Intersection of the two linked lists: \n");

Display(intersec1);

}

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.