Challenge

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

/*
 * linklist.c
 *
 *  Created on: Feb 22, 2023
 *      Author: Muntazir Ahmed
 */

#include<stdio.h>                            //Header file
#include<stdlib.h>
#include<math.h>

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

}Node;                                        //changing the structure name as node

static Node *head1, *head2, *head_union, *head_intersection, *tail, *res_union, *res_intersection, *newnode;  //declaring the variable in structure type

/*declaring different function with the argument*/

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

	}
	else
	{
		tail = head_ref;                   //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 *insert_node(Node *head_ref, int n)        //function uses to create the new list
{
	printf("\nInserting element the beginning....\n ");
	newnode =(Node *)malloc(sizeof(Node));                    //allocating the memory to the nodes
	printf("Address of creating new node : %d\n",newnode);
	newnode ->data = n;                                      //assigning the value of nodes
	printf("inserting value is : %d\n",newnode->data);
	newnode ->next = head_ref;              //address of the previous address is transfered to new nodes
	head_ref = newnode;                     //transfering the new naddress to the head ref for linking new node

	return head_ref;

}
Node *get_union(Node *head_pointer1,Node *head_pointer2)   //function used to union to list
{
	while(head_pointer1 != 0)
	{
		head_union =insert_node(head_union,head_pointer1 ->data);  //Here we are inserting the data os 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 insering the element from list 2 number
			                                               //where not in the in the list 1 to the union

			head_union = insert_node(head_union, head_pointer2 ->data);  //inserting the element not in the list 2
		head_pointer2 = head_pointer2 ->next;                //jump to next node

	}
	return head_union;
}

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 list intersection
			head_intersection = insert_node(head_intersection,head_pointer1 ->data); //transferring the thst 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("Enter %d the element : ",first_list_nodes);  //getting the number of element in the lists
	for (f=0; f<first_list_nodes; f++)
	{
		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);              //displating the head 1 list
	printf("\n Enter the Number of Nodes in second list : ");
	fflush(stdout);
		scanf("%d",&second_list_nodes);
		printf("Enter %d the element : ",second_list_nodes);
		for (f=0; f<second_list_nodes; f++)
		{
			scanf("%d",&second_list_items);   //getting the data in run time
			head2 = insert_node(head2,second_list_items);
		}
		display_list(head2);        //displating the head 2 list
		res_union = get_union(head1,head2);   //Transfering the result of union function to the res union
		printf("Here is the result of union : ");
		display_list(res_union);           //transferring the values asargument to display function
		res_intersection = get_intersection(head1, head2);  //Transfering the result of intersection function to the res intersection

		printf("Here is the result of intersection : "); //transferring the result of intersection function to the res intersection
		display_list( res_intersection);     //transferring the values asargument to display function
}
Output

Enter the number of nodes in first list: 2
3
5
Enter 2 the element : 
Inserting element the beginning....
 Address of creating new node : 13967368
inserting value is : 3

Inserting element the beginning....
 Address of creating new node : 13967384
inserting value is : 5
 5 3
 Enter the Number of Nodes in second list : 9
7
8
6
5
4
3
2
1
9
Enter 9 the element : 
Inserting element the beginning....
 Address of creating new node : 13967400
inserting value is : 7

Inserting element the beginning....
 Address of creating new node : 13967416
inserting value is : 8

Inserting element the beginning....
 Address of creating new node : 13967432
inserting value is : 6

Inserting element the beginning....
 Address of creating new node : 13967448
inserting value is : 5

Inserting element the beginning....
 Address of creating new node : 13967464
inserting value is : 4

Inserting element the beginning....
 Address of creating new node : 13967480
inserting value is : 3

Inserting element the beginning....
 Address of creating new node : 13967496
inserting value is : 2

Inserting element the beginning....
 Address of creating new node : 13967512
inserting value is : 1

Inserting element the beginning....
 Address of creating new node : 13967528
inserting value is : 9
 9 1 2 3 4 5 6 8 7
Inserting element the beginning....
 Address of creating new node : 13967544
inserting value is : 5

Inserting element the beginning....
 Address of creating new node : 13967560
inserting value is : 3

Inserting element the beginning....
 Address of creating new node : 13967576
inserting value is : 9

Inserting element the beginning....
 Address of creating new node : 13967592
inserting value is : 1

Inserting element the beginning....
 Address of creating new node : 13967608
inserting value is : 2

Inserting element the beginning....
 Address of creating new node : 13967624
inserting value is : 4

Inserting element the beginning....
 Address of creating new node : 13967640
inserting value is : 6

Inserting element the beginning....
 Address of creating new node : 13965272
inserting value is : 8

Inserting element the beginning....
 Address of creating new node : 13965336
inserting value is : 7
Here is the result of union :  7 8 6 4 2 1 9 3 5
Inserting element the beginning....
 Address of creating new node : 13965400
inserting value is : 5

Inserting element the beginning....
 Address of creating new node : 13965304
inserting value is : 3
Here is the result of intersection :  3 5

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.