Challenge

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

Overview:

The program is used to find the intersection and union of two linked list.The list is provided by the user.It will find the union and prints the list.For intersection it will give only one value as result.

#include<stdio.h>

#include<stdlib.h>

/* A structure of linked list node */
struct node {
int data;
struct node *next;
} *LLOne, *LLTwo, *unionLL, *intersectionLL;

void initialize(){
LLOne = LLTwo = NULL;
}
/*
Insert a node in a singly linked list.
*/
void insert_node(struct node **head, int num) {
/* Create a new Linked List node */
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
/* Next pointer of new node will point to head node of linked list */
newNode->next = *head;
/* make new node as new head of linked list */
*head = newNode;
}

/* Searches an element in Linked List */

int search(struct node *head, int num) {
while (head != NULL) {
if (head->data == num){
return 1;
}
head = head->next;
}
return 0;
}

/* Returns the union of two given linked list */
struct node* get_union(struct node *LLOne, struct node *LLTwo) {
unionLL = NULL;
/* Add all nodes of first Linked List to unionLL */
struct node *temp = LLOne;
while(temp != NULL){
insert_node(&unionLL, temp->data);
temp = temp->next;
}
/* Insert those nodes of LLTwo which is not present in LLOne */
while(LLTwo != NULL){
if(!search(LLOne, LLTwo->data)){
insert_node(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->next;
}
return unionLL;
}
/*
Returns the Linked List which contains common nodes of two given linked list
*/
struct node* get_intersection(struct node *LLOne, struct node *LLTwo) {
intersectionLL = NULL;
/* Search every element of LLOne in LLTwo,
If found then add it to intersection List */
while(LLOne != NULL){
if(search(LLTwo, LLOne->data)){
insert_node(&intersectionLL, LLOne->data);
}
LLOne = LLOne->next;
}
return intersectionLL;
}
/*Prints a linked list from head */
void display_list(struct node *nodePtr) {
while (nodePtr != NULL) {
printf(" %d ", nodePtr->data);
nodePtr = nodePtr->next;

}
}

int main() {
int i, LLOneCount, LLTwoCount, temp;
initialize();
/* Creating First linked List*/
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLOneCount);
printf("Enter %d integers\n", LLOneCount);
for(i=0; i<LLOneCount; i++){
scanf("%d", &temp);
insert_node(&LLOne, temp);
}
display_list(LLOne);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLTwoCount);
printf("Enter %d integers\n", LLTwoCount);
for(i=0; i<LLTwoCount; i++){
scanf("%d", &temp);
insert_node(&LLTwo, temp);
}
display_list(LLTwo); /* printing Union of two given linked list */
get_union(LLOne, LLTwo);
get_intersection(LLOne, LLTwo);
printf("\nUnion Linked List\n");
display_list(unionLL);
printf("\nIntersection Linked List\n");
display_list(intersectionLL);
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.