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>

// A structure of linked list node
struct node

{
int data;
struct node *next;


} *funA, *funB, *unionfun, *intersectionfun;//using structure

void initialize()

{
funA = funB = NULL;//null character using to avoid the carbage value ;
}
//Given a Inserts a node in front of a singly linked list.

void insert(struct node **head, int num)

{
// Create a new Linked List node
struct node* funNode = (struct node*) malloc(sizeof(struct node));
funNode->data = num;
// Next pointer of funnode will point to head node of linked list
funNode->next = *head;
//make fun node as new head of linked list
*head = funNode;
}

// Searches an element in Linked List by linearly traversing from head to tail
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* findunion(struct node *funA, struct node *funB)

{
unionfun = NULL;
// Add all nodes of first Linked List to unionfun
struct node *temp = funA;
while(temp != NULL)

{
insert(&unionfun, temp->data);
temp = temp->next;
}
// Insert those nodes of funB which is not present in funA
while(funB != NULL)

{
if(!search(funA, funB->data))

{
insert(&unionfun, funB->data);
}
funB = funB->next;
}
return unionfun;
}


//Returns the Linked List which contains common nodes of two given linked list

struct node* intersection(struct node *funA, struct node *funB)

{
intersectionfun = NULL;
//Search every element of funA in funB, If found then add it to intersection List
while(funA != NULL)

{
if(search(funB, funA->data))

{
insert(&intersectionfun, funA->data);
}
funA = funA->next;
}
return intersectionfun;
}
//Prints a linked list from head node till tail node

void printLinkedList(struct node *nodePtr)

{
while (nodePtr != NULL)

{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("<->");

}
}

int main()

{
int i, funACount, funBCount, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &funACount);
printf("Enter %d integers\n", funACount);
for(i=0; i<funACount; i++)

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

{
scanf("%d", &temp);
insert(&funB, temp);
}
printLinkedList(funB);
//printing Union of two given linked list
findunion(funA, funB);
intersection(funA, funB);
printf("\nUnion fun\n");
printLinkedList(unionfun);
printf("\nIntersection fun\n");
printLinkedList(intersectionfun);

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.