Challenge

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

OFFLINE

1.

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}Node;
static Node *head,*tail,*newnode,*head2,*f_union,*f_intersect,*head_union,*head_intersect;
Node*insert_node(Node *head_ref,int n);
void display_list(Node *head_ref);
Node *find_intersect(Node *head_ptr,Node *head_ptr2);
Node *find_union(Node *head_ptr,Node *head_ptr2);
int compare(Node *head1,int data);
int main()
{
int first_list_nodes=0 , first_list_items=0;
printf("Enter the number of lists in the array A:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter all %d Elements:",first_list_nodes);
for(int i=0; i<first_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&first_list_items);
head=insert_node(head,first_list_items);
}
display_list(head);
int second_list_nodes=0 , second_list_items=0;
printf("\n \nEnter the number of lists in the array B:");
fflush(stdout);
scanf("%d",&second_list_nodes);

printf("Enter all %d Elements:",second_list_nodes);
for(int i=0; i<second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items);
head2=insert_node(head2,second_list_items);
}
display_list(head2);
f_union =find_union(head,head2);
printf("\nunion: ");
display_list(f_union);
f_intersect =find_intersect(head,head2);
printf("\nintersection: ");
display_list(f_intersect);
}
Node *insert_node(Node *head_ref,int n)
{
newnode= (Node*) malloc(sizeof(Node));
newnode->data = n;
newnode->next= head_ref;
head_ref=newnode;
return head_ref;
}
void display_list(Node *head_ref)
{
if(head_ref==0)
{
printf("List is empty");
}
else
{
tail=head_ref;
while(tail!=0)
{
printf("%d",tail->data);
tail=tail->next;
}
}
}
Node *find_union(Node *head_ptr,Node *head_ptr2)
{
while(head_ptr != 0)
{
head_union = insert_node(head_union,head_ptr->data);
head_ptr = head_ptr->next;

}
while(head_ptr2 != 0)
{
if(!compare(head_union,head_ptr2->data))
head_union = insert_node(head_union,head_ptr2->data);
head_ptr2 = head_ptr2->next;
}
return head_union;
}
int compare(Node *head,int data)
{
while (head != NULL)
{
if (head->data == data)
return 1;
head = head->next;
}
return 0;
}
Node *find_intersect(Node *head_ptr,Node *head_ptr2)
{
while (head_ptr != 0)
{
if (compare(head_ptr2, head_ptr->data))
head_intersect = insert_node(head_intersect,head_ptr->data);
head_ptr = head_ptr->next;
}
return head_intersect;
}

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.