Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
struct node // structure of linked list
{
int data;
struct node *next;
}
*ListOne, *ListTwo, *unionList, *intersectionList;
void display()
{
ListOne = ListTwo = NULL;
}
void insert(struct node **head, int num) // Inserts a node in front of a singly linked list
{
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;
}
struct node* findunion(struct node *ListOne, struct node *ListTwo)
{
unionList = NULL;
struct node *temp = ListOne;
while(temp != NULL)
{
insert(&unionList, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(ListTwo != NULL)
{
if(!search(ListOne, ListTwo->data))
{
insert(&unionList, ListTwo->data);
}
ListTwo = ListTwo->next;
}
return unionList;
}
//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *ListOne, struct node *ListTwo)
{
intersectionList = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(ListOne != NULL)
{
if(search(ListTwo, ListOne->data))
{
insert(&intersectionList, ListOne->data);
}
ListOne = ListOne->next;
}
return intersectionList;
}
//Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
fflush(stdout);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, ListOneCount, ListTwoCount, temp;
void Display();
//Creating First linked List
printf("Enter number of nodes in first Linked Listn");
fflush(stdout);
scanf("%d", &ListOneCount);
printf("Enter %d integersn", ListOneCount);
fflush(stdout);
for(i=0; i<ListOneCount; i++)
{
scanf("%d", &temp);
insert(&ListOne, temp);
}
printLinkedList(ListOne);
fflush(stdout);
printf("nEnter number of nodes in second Linked Listn");
fflush(stdout);
scanf("%d", &ListTwoCount);
printf("Enter %d integersn", ListTwoCount);
fflush(stdout);
for(i=0; i<ListTwoCount; i++)
{
scanf("%d", &temp);
insert(&ListTwo, temp);
}
printLinkedList(ListTwo);
fflush(stdout);
//printing Union of two given linked list
findunion(ListOne, ListTwo);
intersection(ListOne, ListTwo);
printf("nUnion Linked Listn");
fflush(stdout);
printLinkedList(unionList);
fflush(stdout);
printf("nIntersection Linked Listn");
fflush(stdout);
printLinkedList(intersectionList);
return 0;
}