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>
struct Node //creating structure
{
int data; //to store data

struct Node *next; //store address of next node
}*head_union,*head_intersection,*head_pointer1,*head_pointer2,*head_pointer,*head1,*head2,*res_union,*res_intersection;
struct Node *insert_node(struct Node *headref,int n) //creating insert node function
{
struct Node *newnode;
//printf("inserting elements");
newnode=(struct Node*)malloc(sizeof(struct Node)); //allocating the node
newnode->data=n;
printf("%d\n",newnode->data);
newnode->next=headref; //linking the old list to new node
headref=newnode; //head point to new node
}
void display(struct Node* node) //display function
{
{
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}
}
struct Node *getunion(struct Node *head_pointer1,struct Node *head_pointer2) //get union function
{
while(head_pointer1 != 0)
{
insert_node(head_union,head_pointer1->data); //inserting nodes from list1
head_pointer1=head_pointer1->next;
}
while(head_pointer2 != 0)
{
if(!compare(head_union,head_pointer2->data)) //comparing list1 and not not printing which are on list1
{
insert_node(head_union,head_pointer2->data);
head_pointer2=head_pointer2->next;
}
}
return head_union;
}
int compare(struct Node *head,int data) //compare function
{
while (head !=NULL)
{
if(head->data==data)
{
return 1;
}
head=head->next;
}
return 0;
}
struct Node *getintersection(struct Node *head_pointer1,struct Node *head_pointer2) //intersection function
{
head_intersection=NULL;
while(head_pointer1 !=0) //running while loop
{
if(compare(head_pointer2,head_pointer1->data))
{
insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
}
return head_intersection;
}
int main()
{
int first_node=0,second_node=0,first_item=0,second_item=0; //creating variables
printf("enter the number of nodes in first list:");
fflush(stdout);
scanf("%d",&first_node);
for(int i=0;i<first_node;i++) //running for loop
{
fflush(stdout);
scanf("%d",&first_item);
head1=insert_node(head1,first_item);
}
display(head1); //display function
printf("\nenter the number of nodes in second list: ");
fflush(stdout);
scanf("%d",&second_node);
printf("enter %d of elements:",second_node);
for(int i=0;i<second_node;i++)
{
fflush(stdout);
scanf("%d",&second_item);
head2=insert_node(head2,second_item);
}
display(head2);
printf("here is the result of union:\n"); //printing the result of union
res_union=getunion(head1,head2);

printf("here is the result of intersection: "); //printing the result of intersection
res_intersection=getintersection(head1,head2);
display(res_union);
display(res_intersection);
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.