Challenge

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

1.

#ifndef LI_H_
#define LI_H_

#include<stdio.h>//preprocessor
#include<stdlib.h>//preprocessor
#include<stdbool.h>//preprocessor


typedef struct node // an new name for some existing data type//
{
int data;//local variable//
struct node *next;//declaration of the struct//
}Node;

static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_intersection,*res_union;


Node *insert_node(Node *head_ref,int n);//call the function of insert_node//
void display_list(Node *head_ref);//display the reference//
Node *get_union(Node *linked_list1,Node *linked_list2);//call the all memory location//
Node *get_intersection(Node *linked_list1,Node *linked_list2);//call the a set of variable/
int compare(Node *head,int data);//call the both node data//

#include"li.h" //header//
void display_list(Node *head_ref) //displaying the lsit of the reference in the head//
{
if(head_ref==0) //here the condition is head_ref is equal to the 0//
{
printf(" list is empty"); //above the condition will be satisfied then the printf will be display//
}
else
{
tail=head_ref;
while(tail !=0) //while loop starts tail is not equal to the 0//
{
printf("%d",tail->data); //if condition is not satisfied then the else part will be display.after its will next list//

tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n) //insert the number of nodes//
{

printf("\nInserting Element at the beginning...\n"); //inserting the number of the elements in the console//
newnode=(Node*)malloc(sizeof(Node)); //here giving the size of the malloc node//
printf("Address of created newnode :%d\n",newnode); //printf is the printing address of the malloc//
newnode->data=n; //number of the newnode data//
printf("Inserted value is :%d\n",newnode->data); //value of newnode data will be display on the console//
newnode->next=head_ref;
head_ref=newnode; //reference of the newnode data is equal to head//
return head_ref; //return reference//
}
Node *get_union(Node *linked_list1,Node *linked_list2)//here node of pointer1 and pointer2 can be shared by the same memory location//
{
while(linked_list1 !=0)
{
head_union = insert_node(head_union,linked_list1->data); //inserting the data of the pointer1//
linked_list1=linked_list1->next; //its will go to the next of the value of newpointer1 next
}
while(linked_list2 !=0)
{
if(! compare(head_union,linked_list2->data)) //here comparing the pointer1 and pointer2 of the date//
head_union=insert_node(head_union,linked_list2->data); //inserting the data of the pointer2//
linked_list2=linked_list2->next; //its will go to the next of the value of newpointer2 next
}
return head_union;
}
int compare(Node *head,int data) //compare the both *head data//
{
while(head !=NULL)
{
if(head->data == data)
return 1;
head = head->next;
}
return 0;
}
Node *get_intersection(Node *linked_list1,Node *linked_list2)
{
while(linked_list1 !=0)
{
if( compare(linked_list2,linked_list1->data)) //here comparing both elements are same or not//
head_intersection = insert_node(head_intersection,linked_list1->data); //inserting the data of the pointer1//
linked_list1 =linked_list1->next; //its will go and take next of the value of new pointer1 next//
}
return head_intersection;
}

#include "li.h" //header//
int main() //main function//
{

int first_list_nodes=0,second_list_nodes=0,first_list_items=0, //local variable// here given nodes,items//
second_list_items=0;
printf("\nEnter the number of nodes in first list:"); //printing the node1 list of the console//
fflush(stdout);
scanf("%d",&first_list_nodes); //reads the address//
printf("\nEnter %d the elements:",first_list_nodes);//printing the list1 in the console//
for(int i=0;i<first_list_nodes;i++) //for loop will starts first_list_nodes here//
{
fflush(stdout);
scanf("%d",&first_list_items); //reads the address//
head1=insert_node(head1,first_list_items); //here declaring the first_list_items//
}
display_list(head1); //displaying list of header1//
printf("\nEnter the number of nodes in second list:"); //printing the node2 list of the console//
fflush(stdout);
scanf("%d",&second_list_nodes); //reads the address//
printf("\nEnter %d the elements:",second_list_nodes); //printing the list2 in the console//
for(int i=0;i< second_list_nodes;i++)//for loop will starts second_list_nodes here//
{
fflush(stdout);
scanf("%d",&second_list_items);//here declaring the second_list_items//
head2=insert_node(head2,second_list_items);//here declaring the second_list_items//
}

display_list(head2);//displaying list of header1 and header2//
res_union = get_union(head1,head2);//result of the head1 and head2 will store the memory of the same location//


printf("Here is the result of union:"); //end of the result will be display in the console//
display_list(res_union);
res_intersection = get_intersection(head1,head2);//result of the get intersection of head1 and head2//

printf("\nHere is the result of intersection:");//endif is the final result will be display in the console//
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.