Challenge

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

/*
* ll.c
*
* Created on: 14-Dec-2022
* Author: ll.c
*/


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node//defining the structure with duplicate datatype by using typedef.
{
int data;//creating the data type of the variable
struct node *next;// creating self node
}Node;//node is the structure variable

static Node *g,*h,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;//declaring the different variable in structure type
/* Declaring diffferent function with the arguments*/
Node *insert_node(Node *head_ptr,int n);
void display_list(Node *head_ptr);
Node *get_union(Node *head_pointer1,Node *head_pointer2);
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);
int compare(Node *head,int data);
Node *insert_node(Node *head_ptr,int n)//function used to create the new linked list
{
printf("\nInserting Elements at the beginning...\n");
newnode =(Node*) malloc(sizeof(Node));//allocating the memory to the new node
newnode->data = n;//assigning the value to node
printf("Inserted value is :%d\n",newnode->data);
newnode->next = head_ptr;//address of the pervious address is transfered to the new node
head_ptr = newnode;//transfering the new address to the head ref for linking new node
return head_ptr;
}
void display_list(Node *head_ptr)//traverse function used to display the nodes data .
{
if(head_ptr == 0)
{
printf(" List is empty.");
}
else
{
tail = head_ptr;//we are transfering address if the list to tail
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 *get_union(Node *head_pointer1,Node *head_pointer2)//function used to union to list
{
while(head_pointer2 != 0)
{
head_union = insert_node(head_union,head_pointer2->data);//here we are inserting the data of list 1 into the union function
head_pointer2 = head_pointer2->next;//we transverse the node using the address in next.

}
while(head_pointer1 != 0)
{
if(!compare(head_union,head_pointer1->data))//Here we are inserting the element from list 2 number where not in the in th list1 to the union
head_union = insert_node(head_union,head_pointer1->data);//inserting the element not in the list2
head_pointer1 = head_pointer1->next;//travelling to next node
}
return head_union;
}
/*here find the same elements with position at lists*/
int compare(Node *head,int data)
{
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)
{
while (head_pointer1 != 0)
{
if (compare(head_pointer2, head_pointer1->data))//we are comparing the both lsit for instersection
head_intersection = insert_node(head_intersection,head_pointer1->data);//transferring the that value to the intersection
head_pointer1 = head_pointer1->next;


}
return head_intersection;
}
int main()
{

int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,
second_list_items = 0;
int f;
printf("Enter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("%d Enter the elements:",first_list_nodes);//getting the number of element in the lists
for( f = 0;f < first_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&first_list_items);//getting the data in run time
g= insert_node(g,first_list_items);//inserting the data in list
}
display_list(g);//displaying the head1 list
printf("\nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter %d the elements:",second_list_nodes);
for(f = 0;f < second_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&second_list_items);//getting the data in run time
h = insert_node(h,second_list_items);
}
display_list(h);//displaying the head1 list
res_union = get_union(g,h);//transfering the result of union function.
printf("Here is the result of union: ");
display_list(res_union);//transfering the values as arguments to display function
res_intersection = get_intersection(g,h);//transfering the result of intersection function to the res intersection

printf("Here is the result of intersection: ");//transfering the result of intersection function to the res intersection
display_list(res_intersection);//transfering the values as arguments to display function

}

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.