Challenge

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

1)

#include <stdio.h>
#include <stdlib.h>

struct node

{
int data;
struct node *next;


} *First_list, *Second_list, *union_list, *intersection_list; // structure

void initialize()

{
First_list = Second_list = NULL;
}

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

{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = *head; // point to head
*head = newNode; // for next iteration
} // to add new element to list


int search(struct node *head, int num)

{
while (head != NULL)

{
if (head->data == num)

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

}
//to search element from node

struct node* findunion(struct node *First_list, struct node *Second_list)

{
union_list = NULL;
struct node *temp = First_list;
while(temp != NULL)

{
insert(&union_list, temp->data);
temp = temp->next;
}
while(Second_list != NULL)

{
if(!search(First_list, Second_list->data))

{
insert(&union_list, Second_list->data);
}
Second_list = Second_list->next;
}
return union_list;
}
//to get union

struct node* intersection(struct node *First_list, struct node *Second_list)

{
intersection_list = NULL;
while(First_list != NULL)

{
if(search(Second_list, First_list->data))

{
insert(&intersection_list, First_list->data);
}
First_list = First_list->next;
}
return intersection_list;
}
//to get intersection

void printLinkedList(struct node *nodePtr)

{
while (nodePtr != NULL)

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

int main()

{
int i, First_listCount, Second_listCount, temp;
initialize();
//Creating First linked List
printf("No. of elements List\n");
fflush(stdout);
scanf("%d", &First_listCount);
printf("Enter %d elements\n", First_listCount);
fflush(stdout);
for(i=0; i<First_listCount; i++)

{
scanf("%d", &temp);
insert(&First_list, temp);
}
printLinkedList(First_list);
printf("\n No. elements for second Linked List\n");
fflush(stdout);
scanf("%d", &Second_listCount);
printf("Enter %d elements\n", Second_listCount);
fflush(stdout);
for(i=0; i<Second_listCount; i++)

{
scanf("%d", &temp);
insert(&Second_list, temp);
}
printLinkedList(Second_list);
//printing Union of two given linked list
findunion(First_list, Second_list);
intersection(First_list, Second_list);
printf("\nUnion = \n");
fflush(stdout);
printLinkedList(union_list);
printf("\n Intersection = \n");
fflush(stdout);
printLinkedList(intersection_list);

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.