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
#include
#include

//creating node
typedef struct node{//defining structure
int data;
struct node *next;
}Node;//changing the structure name as 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 node 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)
{
printf("\n insert elements at the beginning \n");
newnode=(Node*)malloc(sizeof(Node));
printf("address of created newnode: %d\n ",newnode);
newnode->data=n;
printf("inserted value is %d\n",newnode->data);
newnode->next=head_ref;
return head_ref;
}


Node *get_union(Node*head_pointer1,Node*head_pointer2)
{
while(head_pointer1 != 0)
{
head_union = insert_node(head_union,head_pointer1->data);
head_pointer1 = head_pointer1 -> next;
}
while(head_pointer2 != 0)
{
if(!compare(head_union,head_pointer2->data))
head_union = insert_node(head_union,head_pointer2 -> data);
head_pointer2 = head_pointer2 -> next;
}
return head_union;
}


Node*get_intersection(Node*head_pointer1,Node *head_pointer2)
{
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;
}
}


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


int main()
{
int first_list_nodes=0,second_list_nodes=0,first_list_items=0,second_list_items=0;
int f;
printf("enter number of nodes in first list ");//first list input
scanf("%d",&first_list_nodes);//total nodes
printf("enter %d the elements: ",first_list_nodes);//second list input
for(f=0;f scanf("%d",&first_list_items);
head1=insert_node(head1,first_list_items);//storing first list items in head1
}
display_list(head1);
printf("\n ENTER THE NUMBER OF NODES IN SECOND LIST ");
scanf("%d",&second_list_nodes);
printf("enter %d the elements:",second_list_nodes);
for(f=0;f < second_list_nodes; f++)
{
scanf("%d",&second_list_nodes);
head2=insert_node(head2,second_list_items);//storing second list items in head2
}
display_list(head2);
res_union =get_union(head1,head2);//res_union stores union of head1 and head2
printf("the result of the union ");
display_list(res_union);//displaying the union
res_intersection=get_intersection(head1,head2);//res_intersection stores intersection of head1 & head2
printf("the result of intersection");
display_list(res_intersection);//displaying the 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.