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
// A structure of linked list node
struct node
{
int data;
struct node* next;
} *LLOne, *LLTwo, *unionLL, *intersectionL
void initialize()
{
LLOne = LLTwo=NULL;
}
// insert a node in a singly linked list
void insert(struct node** head, int num)
{
//create a new linked list
struct node*newNode=(struct node*)malloc(sizeof(struct node));
newNode-> data=num;
//next pointer of new node will point to head node of linked list
newNode->next=*head;
// search an element in linked list by linerly 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 union of two linked list
struct node* findunion(struct node *LLOne, struct node *LLTwo)
{
unionLL=NULL;
// add all nodes of first linked list to uniomll
struct node* temp=LLOne;
while(tmp!=NULL)
{
insert(&unionLL, temp-> data);
temp=temp-> next;
}
// insert those nodes of LLTwo
while(LLTwo!=NULL)
{
if(!search(LLOne, LLTwo-> data))
{
insert(&union,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;
// search every element of LLOne in LLTwo, if found then ADD in intersectionl list
while(LLOne!=NULL)
{
if(!search(LLTwo, LLOne-> data))
{
insert(&intersectionLL, LLOne-> data);
}
LLOne=LLOne->next;
}
return intersectionLL;
}
// print a linked list from head node to tail node
void printLinkedList(struct node *nodePtr)
{
while(nodePtr!=NULL)
{
printf("%d",nodePtr->data);
nodePtr=nodePtr->next;
if(nodePtr!=NULL)
printf("--->");
}
}
int main()
{
int i, LLOneCount,LLTowCount,temp;
initialize();
//creating first linked list
printf("Enter number of nodes in first linked list\n");
scanf("%d", &LLOneCount);
printf("Enter %d integers\n", LLOneCount);
for(i=0; i { scanf("%d", &temp); insert(&LLOne, temp); } printLinkedList(LLOne); printf("\n Enter number of nides in second linked list\n"); scanf("%d"&LLTwoCount); printf("Enter %d intergers\n",LLTwoCount); for(i=0; i { scanf("%", &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"); printLinkedList(unionLL); printf("\n intersection Linked List\n"); printLinkedList(intersectionLL); return 0; }