Challenge

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

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

Code:

set theory.h file

#ifndef SET_THEORY_H_
#define SET_THEORY_H_

typedef struct node//node representation
{
int data;//data of the node
struct node *next;//address of the next node
}set_theory;

set_theory *LLone, *LLtwo, *unionLL, *intersectionLL;

void insert(struct node **head, int num);
int search(struct node *head, int num);
struct node* getunion(struct node *LLone, struct node *LLtwo);
struct node* intersection(struct node *LLone, struct node *LLtwo);
void printLinkedList(struct node *ptr);

#endif /* SET_THEORY_H_ */

set theory.c file

#include"set theory.h"
#include<stdio.h>
#include<stdlib.h>

void insert(struct node **head, int num)
{
struct node *newNode = (struct node*) malloc(sizeof(struct node));//create a new linked list node
newNode->data = num;
newNode->next = *head;//next pointer of new node will point to head node of linked list
*head = newNode;//make new node as new head of linked list.
}

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

struct node* getunion(struct node *LLone, struct node *LLtwo)
{
unionLL = NULL;//initialize unionLL with NULL
//add all nodes of first linked list to unionLL
struct node *temp = LLone;
while (temp != NULL) {
insert(&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(&unionLL, LLtwo->data);
}
LLtwo = LLtwo->next;
}
return unionLL;
}

struct node* intersection(struct node *LLone, struct node *LLtwo)
{
intersectionLL = NULL;//initialize intersectionLL with NULL
//Search every elements of LLone in LLtwo,if found then add it to intersection list.
while (LLone != NULL)
{
if (search(LLtwo, LLone->data))
{
insert(&intersectionLL, LLone->data);
}
LLone = LLone->next;
}
return intersectionLL;
}

void printLinkedList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%d\t ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

main.c file

#include"set theory.h"
#include<stdio.h>//preprocessor section.
#include<stdlib.h>//header file is used to declare number conversions,storage allocations etc.

int main()
{
int i, num, choice, temp;

//creating first linked list
printf("Enter number of nodes in first linked list: ");
fflush(stdout);
scanf("%d", &num);

printf("\nEnter %d data:", num);
fflush(stdout);
for (i = 0; i < num; i++) {
scanf("%d", &temp);
insert(&LLone, temp);
}
printLinkedList(LLone);
//creating second linked list
printf("\nEnter number of nodes in second linked list: ");
fflush(stdout);
scanf("%d", &num);
printf("\nEnter %d data:", num);
fflush(stdout);
for (i = 0; i < num; i++) {
scanf("%d", &temp);
insert(&LLtwo, temp);
}
printLinkedList(LLtwo);

while(choice!=3)
{
printf("\n1.Union of Linked Lists\t2.Intersection of Linked Lists\t3.Exit\n");
fflush(stdout);
scanf("%d",&choice);
switch(choice)
{
case 1:
getunion(LLone, LLtwo);
printf("\nUnion of linked list: \n");
printLinkedList(unionLL);
break;
case 2:
intersection(LLone, LLtwo);
printf("\nIntersection of linked list: \n");
printLinkedList(intersectionLL);
break;
case 3: exit(0);
break;
default: printf("Enter valid choice! \n");
fflush(stdout);
}
}
return 0;
}

Result:

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.