Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
#include
#include
struct node
{
int data;
struct node *next;
}
LL*one,LL*two,*unionLL,*intersection LL;
void initilaize ()
{
LLone=LLtwo=NULL; //Given a inserts a node in singly linked list//
void insert (struct node *head,int num);
{
struct node*newnode=(structnode*)malloc(sizeof (struct node)); //create a new linked list node//
newnode->data=num; //next pointer of newnode will point to head node of a linked list//
newnode->next=*head;
*head=newnode;
}
//searches 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* find union (struct node*LLone,struct node*LLtwo)
{
union LL=NULL;
//Add all nodes of first linkedlist to union LL
struct node *temp==LLone;
while(temp!=NULL)
{
insert(&unionLL,temp->data);
temp=temp->next;
}
//insert these nodes of LLtwo which is not present in LLone
while(LLtwo!=NULL)
{
if(search(LLOne,LLtwo,->data)
{
insert(&unionLL,LLtwo->data);
}
LLtwo=LLtwo->next;
}
return unionLL;
}
structnode*intersection(structnode*LLOne,structnode*LLtwo);
{
intersectionLL=NULL;
while(LLone!=NULL);
{
if(search(LLtwo,LLone,->data))
{
insert(&intersectionLL,LLone,->data);
}
LLOne=LLone->next;
}
return intersection LL;
void printlinkedlist(structnode *node ptr)
{
while(nodeptr !=NULL)
{
print("%d",nodeptr->data);
nodeptr=nodeptr->next;
if(nodeptr!=NULL)
print("-->");
}
}
int main()
{
int i,LLOneCount,LLtwoCount,temp;
initialize();
//creating first linkedlist//
printf("enter the no.of nodes in the first linked list n");
scanf("%d",&LLoneCount);
printf("enter %d integersn",LLoneCount);
for(i=0;i { scanf("%d",&temp); insert(&LLOne,temp); } printlinkedlist(LLOne)); printf("enter the no.of nodes in second linked list n"); scanf("%d",LLtwoCount); printf("enter %d integers n",LLtwoCount); for(i=0;i scanf("%d",&temp); insert(&&LLtwo,temp); } printlinkedlist(LLtwo); findunion(LLone,LLtwo); intersection(LLOne,LLtwo); printf("nunionlinked listn); printlinkedlist("nintersection linked list n:"); printlinkedlist(intersection LL); return 0; }