Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Q1.
Code program:
#include
#include
/* a structure of linked list node */
struct node
{
int data;
struct node *next;
} *LLOne, *LLTwo, *unionLL, *intersectionLL;
//function declaration
void initialize()
{
LLOne = LLTwo = NULL;
}
/* Given a inserts a node in front of a singly linked list */
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 */
}
/* search 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 *LLOne, struct node *LLTwo)
{
unionLL = NULL;
struct node *temp = LLOne; /* add all nodes of first linked list to unionLL */
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->next;
}
while(LLTwo != NULL) /* insert those nodes of LLTwo which is not present in LLOne */
{
if(!search(LLOne, LLTwo->data))
{
insert(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->next;
}
return unionLL;
}
/* return the linked list which contains common nodes of two given linked list */
struct node* intersection(struct node *LLOne, struct node *LLTwo)
{
intersectionLL = NULL;
while(LLOne != NULL) /* search every element of LLOne in LLTwo, if found then add it to intersection list */
{
if (search(LLTwo, LLOne->data))
{
insert(&intersectionLL, LLOne->data);
}
LLOne = LLOne->next;
}
return intersectionLL;
}
/* print 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("-->");
fflush(stdout);
}
}
int main()
{
int i, LLOneCount, LLTwoCount, temp;
initialize();
/* Creating first linked list*/
printf("enter number of nodes in first linked list \n");
fflush(stdout);
scanf("%d", &LLOneCount);
printf("enter %d integers \n", LLOneCount);
fflush(stdout);
for(i=0; i<LLOneCount; i++)
{
scanf("%d", &temp);
insert(&LLOne, temp);
}
printLinkedList(LLOne);
printf("\n Enter number of nodes in second linked list \n");
fflush(stdout);
scanf("%d", &LLTwoCount);
printf("enter %d integers \n", LLTwoCount);
fflush(stdout);
for(i=0; i<LLTwoCount; i++)
{
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo);
/*printing union of two given linked list */
findunion(LLOne, LLTwo);
intersection(LLOne, LLTwo);
printf("\n Union linked list \n");
fflush(stdout);
printLinkedList(unionLL);
printf("\n Intersection linked list \n");
fflush(stdout);
printLinkedList(intersectionLL);
fflush(stdout);
return 0;
}
Result:
