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;
}*l1, *l2,*intL, *unionL;

void initialize(){
l1=NULL;
l2=NULL;
}

void insert(struct node **head, int num){
struct node* newNode=(struct node*)malloc(sizeof(struct node));
newNode->data = num;
newNode->next = *head;
*head = newNode;
}

int search(struct node *head, int num) {
while (head != NULL) {
if (head->data == num){
return 1;
}
head = head->next;
}
return 0;
}
//Code for union
struct node* findunion(struct node *l1, struct node *l2) {
unionL = NULL;
/* Add all nodes of first Linked List to unionLL */
struct node *temp = l1;
while(temp != NULL){
insert(&unionL, temp->data);
temp = temp->next;
}
/* Insert those nodes of LLTwo which is not present in LLOne */
while(l2 != NULL){
if(!search(l1, l2->data)){
insert(&unionL, l2->data);
}
l2 = l2->next;
}
return unionL;
}
//Code for intersection
struct node* intersection(struct node *l1, struct node *l2) {
intL = NULL;
/* Search every element of LLOne in LLTwo,
If found then add it to intersection List */
while(l1 != NULL){
if(search(l2, l1->data)){
insert(&intL, l1->data);
}
l1 = l1->next;
}
return intL;
}

void printLinkedList(struct node *nodePtr) {
while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main() {
int i, a, b, temp;
initialize();

printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d", &a);
printf("Enter %d integers\n", a);
fflush(stdout);
for(i=0; i<a; i++){
scanf("%d", &temp);
insert(&l1, temp);
}
printLinkedList(l1);

printf("\nEnter number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d", &b);
printf("Enter %d integers\n", b);
fflush(stdout);
for(i=0; i<b; i++){
scanf("%d", &temp);
insert(&l2, temp);
}
printLinkedList(l2);

findunion(l1,l2);
intersection(l1, l2);

printf("\nUnion Linked List\n");
printLinkedList(unionL);
printf("\nIntersection Linked List\n");
printLinkedList(intL);

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.