Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

#include<stdio.h>

#include<stdlib.h>

//link list node

struct node{

int data;

struct node* next;

};

//function declaration

void inode(struct node** head_ref,int data_ref);

int idata(struct node* head,int data);

//function to get union of two linked list

struct node* getunion(struct node* head1,struct node* head2)

{

struct node* result=NULL;

struct node *L1=head1,*L2=head2;

//insert list 1 to the result

while(L1 !=NULL)

{

inode(&result,L1->data);

L1=L1->next;

}

//insert list 2 to the result

while(L2 !=NULL)

{

if(!idata(result,L2->data))

{

inode(&result,L2->data);

}

L2=L2->next;

}

return result;

}

//function to get intersection of to linked list

struct node* getintersection(struct node* head1,struct node* head2)

{

struct node* result=NULL;

struct node* L1=result;

//search each elements in list2,if the element present in the list 2,then insert the elements to the result

while(L1 !=NULL)

{

if(idata(head2,L1->data))

{

inode(&result,L1->data);

}

L1=L1->next;

}

return result;

}

//a utility function to insert a node at the begging of the list

void inode(struct node** head_ref,int data_ref)

{

//allocate node

struct node* new_node=(struct node*)malloc(sizeof(struct node));

//put in the data

new_node->data=data_ref;

//link the old list of the new node

new_node->next=(*head_ref);

//move the head to point to new node

(*head_ref)=new_node;

}

//function to print a linked list

void printlist(struct node* node)

{

while(node!=NULL)

{

printf("%d",node->data);

node=node->next;

}

}

//function that returns if data is present in linked list else return false

int idata(struct node* head,int data)

{

struct node* t=head;

while (t!=NULL)

{

if(t->data==data)

{

return 1;

}

t=t->next;

}

return 0;

}

//program to test abouve function

int main()

{

int n,m,temp;

printf("enter the number of value in list 1 and list 2 :");

fflush(stdout);

scanf("%d %d",&n,&m);

//start pointer with empty list

struct node* head1=NULL;

struct node* head2=NULL;

struct node* intersecn=NULL;

struct node* unin=NULL;

//for loop for link list1

for(int i=0;i<n;i++)

{

printf("Enter the value in link list 1 %d:",i);

fflush(stdout);

scanf("%d",&temp);

inode(&head1,temp);

}

//for loop for link list2

for(int i=0;i<m;i++)

{

printf("Enter the value in link list 2 %d :",i);

fflush(stdout);

scanf("%d",&temp);

inode(&head2,temp);

}

//call function of intersection

intersecn=getintersection(head1,head2);

//call the function of union

unin=getunion(head1,head2);

printf("\n First list is :");

//function calling with structure pointer as argument

printlist(head1);

printf("\nsecond list is :");

printlist(head2);

printf("\n intersection list is :");

printlist(intersecn);

printf("\n union list is :");

printlist(unin);

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.