Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
LIVE BATCH
In The following program a linked list structure is created and 4 linked lists ll1, ll2, llunion and llintersection are created to save linked lists.
ll1 and ll2 are 2 linked list onto which datas are added and derivables of project union and intersection are stored in llunion and llintersection. no of nodes in each linked list are inputed by user and linked lists are created using insert function where new nodes are created for linked list.
Then union and intersection are sorted out from the both linked lists using find function and results are displayed.
Program
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *ll1, *ll2, *llunion, *llintersection;
int find(struct node *head, int num)
{
while (head != NULL)
{
if (head->data==num)
{
return 1;
}
head = head ->next;
}
return 0;
}
void displayll(struct node *nodeptr)
{
while(nodeptr != NULL)
{
printf("%d", nodeptr->data);
nodeptr = nodeptr->next;
if (nodeptr != NULL)
printf("-->");
}
}
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; //Next pointer node will point to head node of linked list
newNode-> next = *head; //make new node as new head of linked list
*head = newNode;
}
struct node* makeunion(struct node *ll1, struct node *ll2)
{
llunion=NULL;
struct node *temp = ll1;
while (temp != NULL)
{
insert(&llunion, temp->data);
temp=temp->next;
}
while (ll2!=NULL)
{
if (!find(ll1,ll2->data))
{
insert(&llunion, ll2->data);
}
ll2=ll2->next;
}
return (llunion);
}
struct node* makeintersection(struct node *LL1, struct node *LL2)
{
llintersection = NULL;
while(LL1 != NULL)
{
if(find(LL2, LL1->data)) //find every element of LLOne in LLTwo, If found then add it to intersection List
{
insert(&llintersection, LL1->data);
}
LL1 = LL1->next;
}
return llintersection;
}
int main()
{
int i, ll1n,ll2n,temp; //ll1n and ll2n for no of nodes, temp variable to store data in linked list
printf("Enter number of nodes in 1st Linked List \n");
fflush(stdout);
scanf(" %d ", &ll1n);
fflush(stdout);
printf("\n Enter the data for 1st Linked list %d \n" ,ll1n);
for (i=0; i<ll1n;i++)
{
fflush(stdout);
scanf("%d", &temp);
insert(&ll1,temp); //
}
displayll(ll1);
printf("\n Enter number of nodes in 2nd Linked List \n");
fflush(stdout);
scanf(" %d ", &ll2n);
printf("\n Enter the data for 1st Linked list %d \n" ,ll2n);
for (i=0; i<ll2n;i++)
{
fflush(stdout);
scanf("%d", &temp);
insert(&ll2,temp);
}
displayll(ll2);
makeunion(ll1, ll2);
makeintersection(ll1,ll2);
printf(" \n Union Linked List \n");
displayll(llunion);
printf(" \n Intersection Linked List \n");
displayll(llintersection);
return 0;
}
Output
