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 // A structure of linked list node
{
int data;
struct node *next;
} *firstlink, *secondlink, *unionlink, *intersectionlink;
void initialize() //initiallize node creation
{
firstlink = secondlink = NULL;
} //Given a Inserts a node in front of a singly linked list.
void insert(struct node **head, int num_data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // Create a new Linked List node
newNode->data = num_data; // Next pointer of new node will point to head node of linked list
newNode->next = *head; //make new node as new head of linked list
*head = newNode;
}
int search(struct node *head, int num_data) // Searches an element in Linked List by linearly traversing from head to tail
{
while (head != NULL) //continuous search
{
if (head->data == num_data) //condition comparision for first and second linked list all data
{
return 1;
}
head = head->next;
}
return 0;
} //Returns the union of two given linked list
struct node* findunion(struct node *firstlink, struct node *secondlink)
{
unionlink = NULL; // Add all nodes of first Linked List to unionlink
struct node *temp = firstlink;
while(temp != NULL)
{
insert(&unionlink, temp->data);
temp = temp->next;
} // Insert those nodes of second which is not present in first
while(secondlink != NULL)
{
if(!search(firstlink, secondlink->data))
{
insert(&unionlink, secondlink->data);
}
secondlink = secondlink->next;
}
return unionlink;
} //Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *firstlink, struct node *secondlink)
{
intersectionlink = NULL; //Search every element of first in second, If found then add it to intersection List
while(firstlink != NULL)
{
if(search(secondlink, firstlink->data))
{
insert(&intersectionlink, firstlink->data);
}
firstlink = firstlink->next;
}
return intersectionlink;
} //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()
{ { { OUTPUT
int i, firstlinkCount, secondlinkCount, temp;
initialize(); //Creating Function Call - First linked List
printf("Enter number of nodes in first Linked List\n"); //Entering number of nodes
scanf("%d", &firstlinkCount);
printf("Enter %d integers\n", firstlinkCount); //count integers
for(i=0; i
scanf("%d", &temp);
insert(&firstlink, temp); //Function Call for inserting the values
}
printLinkedList(firstlink); //Function call printing first linked node
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &secondlinkCount);
printf("Enter %d integers\n", secondlinkCount);
for(i=0; i
scanf("%d", &temp);
insert(&secondlink, temp);
}
printLinkedList(secondlink); //printing second linked data
findunion(firstlink, secondlink); //finding union function call
intersection(firstlink, secondlink); //finding intersection function call
printf("\nUnion Linked List\n");
printLinkedList(unionlink); //Function call printing of union linked list
printf("\nIntersection Linked List\n");
printLinkedList(intersectionlink); //Function call printing of intersection linked list
return 0;
}