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)program

#include<stdio.h>//preprocessor
#include<stdlib.h>
struct node// linked list node structure
{
int data;
struct node*next;
}
*LLSet1,*LLSet2,*unionLL,*intersectionLL;
void initialize()
{
LLSet1 = LLSet2 = NULL;
}
void insert(struct node**head, int num)//inserting node singly linked list
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));//creating new linked list node
newNode->data = num;
newNode->next = *head;//new node pointer will point to head node of linked list
*head = newNode;//assigning new node as new head
}
int search(struct node*head,int num)//searching element in the linked list
{
while(head != NULL)
{
if(head->data==num)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* findunion(struct node *LLSet1,struct node *LLSet2)//finding union of two given linked list
{
unionLL = NULL;
struct node *temp = LLSet1;//adding nodes of first linked list to union linked list
while(temp!=NULL)
{
insert(&unionLL,temp->data);
temp = temp->next;
}
while(LLSet2 != NULL)//adding nodes of linked list set2 which not present in set1
{
if(!search(LLSet1,LLSet2->data))
{
insert(&unionLL,LLSet2->data);
}
LLSet2 = LLSet2->next;
}
return unionLL;
}
struct node* intersection(struct node *LLSet1,struct node *LLSet2)//it returns the linked list which contain common nodes of two given linked list
{
intersectionLL = NULL;
while(LLSet1 != NULL)
{
if(!search(LLSet2,LLSet1->data))
{
insert(&intersectionLL,LLSet1->data);
}
LLSet1 = LLSet1->next;
}
return intersectionLL;
}
void printLinkedlist(struct node *nodePtr)//print linked list from head node
{
while(nodePtr != NULL)
{
printf("%d",nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("--");
}
}
int main()//main function
{
int i,LLSet1,LLSet2,temp;
initialize();
printf("enter number of nodes in first linkedlist/n");//creating first linked list
scanf("%d",&LLSet1);
printf("enter the integers is %d/n",LLSet1);
for(i=0;i<LLSet1;i++)
{
scanf("%d",&temp);
insert(&LLSet1,temp);
}
printLinkedlist(LLSet1);
printf("enter number of nodes in second linkedlist/n");//creating second linked list
scanf("%d",&LLSet2);
printf("enter the integer is %d/n",LLSet2);
for(i=0;i<LLSet2;i++)
{
scanf("%d",&temp);
insert(&LLSet2,temp);
}
printLinkedlist(LLSet2);
findunion(LLSet1,LLSet2);
printf("union Linkedlist/n");//printing union of given two linked list
printLinkedlist(unionLL);
intersection(LLSet1,LLSet2);//printing common nodes of given two linked list
printf("intersection Linkedlist/n");
printLinkedlist(intersectionLL);
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.