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>
#include<stdbool.h>

typedef struct node//defining the structure
{
int data;// creating the data type of the variable
struct node *next;// creating self referential node

}Node;

// Declaring the different varable in structure type
static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;

/*Declaring different function with arguments*/

Node *insert_node(Node *head,int n);

void display_list(Node *head);

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) // function used to display the nodes data
{
if(head == 0)
{
printf(" List is empty. ");

}
else
{
tail=head; // we are transferring address if the list to head
while(tail!= 0)
{
printf("%d",tail->data); // prints the data of current node
tail = tail->next; // advances the position of current node
}
tail=newnode;

}
}
Node *insert_node(Node *head, int n) // function used create the new list

{

newnode =(Node*)malloc(sizeof(Node)); //Creating new node using malloc
newnode->data = n;
newnode->next = head;
head=newnode;
return head;

}

Node *get_union(Node *head_pointer1,Node *head_pointer2) //function to get the union
{
while(head_pointer1 !=0)
{
head_union = insert_node(head_union,head_pointer1->data); //here we are inserting the data of list 1 into the union function
head_pointer1 = head_pointer1->next; //we transverse the node using the address in next.
}
while(head_pointer2 != 0){
if(!compare(head_union,head_pointer2->data)) //Here we are inserting the element from list 2 number where in the list1 to the union
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next;
}
return head_union;
}

int compare(Node *head,int data) // Used to compare
{
while (head!= NULL)
{
if (head->data == data) //comparing the data of list 2 with list 1
return 1;
head = head->next;
}
return 0;
}

Node *get_intersection(Node *head_pointer1,Node *head_pointer2) //To get the interscetion of two nodes
{
while (head_pointer1 != 0)
{
if (compare(head_pointer2, head_pointer1->data)) //we are comparing the both list for intersection
head_intersection = insert_node(head_intersection,head_pointer1->data); //transferring the value to the intersection
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}

int main() //Main function
{

int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,second_list_items = 0;
int i;
printf("nEnter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("nEnter %d the elements:", first_list_nodes); //getting the number of element in the lists

for(i=0;i<first_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&first_list_items); //getting the data in run time
head1 = insert_node(head1,first_list_items); //inserting the data in list
}
display_list(head1); //displaying the head1 list

printf("nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("nEnter %d the elements:", second_list_nodes);
fflush(stdout);

for(i=0;i<second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items); //getting the data in the run time
head2 = insert_node(head2, second_list_items);
}
display_list(head2); //displaying the head2 list

res_union = get_union(head1 , head2); //transferring the result of union function to the res union
printf("nHere is the result of union: ");
fflush(stdout);
display_list(res_union);

res_intersection = get_intersection(head1, head2); //transferring the result of intersection to the res intersection
printf("nHere is the result of intersection: ");
fflush(stdout);
display_list(res_intersection); //transferring the values as arguments to display function
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.