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>

typedef struct node //structure of node
{
int data;
struct node *next;
}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;
Node *insert_node(Node *head_ref,int n);
void display_list(Node *head_ref);
Node *get_union(Node *head_pointer1,Node *head_pointer2);
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);
int compare(Node *head,int data);
void display_list(Node *head_ref) //display function
{
if(head_ref == 0)
{
printf(" List is empty.");
}
else
{
tail = head_ref;
while(tail != 0)
{
printf("%d ", tail->data);
tail = tail->next;
}
tail = newnode;
}
}

Node *insert_node(Node *head_ref,int n){ //insert function

newnode =(Node*) malloc(sizeof(Node));
newnode->data = n;
newnode->next = head_ref;
head_ref = newnode;
return head_ref;
}

Node *get_union(Node *head_pointer1,Node *head_pointer2) //union set function
{
while(head_pointer1 != 0)
{
head_union = insert_node(head_union,head_pointer1->data); //insert function call
head_pointer1 = head_pointer1->next;

}
while(head_pointer2 != 0)
{
if(!compare(head_union,head_pointer2->data)) //compare function call
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next;
}
return head_union;
}

int compare(Node *head,int data) //compare function
{
while (head != NULL)
{
if (head->data == data)
return 1;
head = head->next;
}
return 0;
}


Node *get_intersection(Node *head_pointer1,Node *head_pointer2) //intersection function
{
while (head_pointer1 != 0)
{
if (compare(head_pointer2, head_pointer1->data))
head_intersection = insert_node(head_intersection,head_pointer1->data);
head_pointer1 = head_pointer1->next;
}

return head_intersection;
}
int main() //main function
{

int n = 0,m = 0,set1 = 0,
set2 = 0;
int f;
printf("enter the nodes for set1 :");
fflush(stdout);
scanf("%d",&n);

for( f = 0;f < n;f++)
{
printf("set1 data for node %d",f);
fflush(stdout);
scanf("%d",&set1);
head1 = insert_node(head1,set1);
}
display_list(head1);
printf("\nenter the nodes for set2 :");
fflush(stdout);
scanf("%d",&m);

for(f = 0;f < m;f++)
{
printf("set1 data for node %d",f);
fflush(stdout);
scanf("%d",&set2);
head2 = insert_node(head2,set2);
}
display_list(head2);
res_union = get_union(head1,head2);
printf("\nunion set: ");
display_list(res_union);
res_intersection = get_intersection(head1,head2);
printf("\n intersection set: ");
display_list(res_intersection);

}

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.