Challenge

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

Project 1:

Creation of user defined data type to implement the user interfaces for working with 'set'. (Mathematical Set Theory)

Introduction:

Head pointer: It points at the starting address of list.

Tail pointer: It is a temporary pointer mainly used for traversing in the list.

Newnode Pointer: It points to the new created node.

Implementation of functions:

In this project, set data-type is implemented using linked list.

  • insert_node() will insert an element to the list.
  • display_list() will display all the elements pree=sent in the list.
  • get_union() will return a list after performing union.
  • get_intersection() will return alist after performing intersection operation between two lists.
  • compare() will compare two linked lists.

Steps to follow:

  • Create two list using insert()_node()function
  • Display the created lists using display_list() function
  • Perfrom union operation
  • Perfrom intersection operation
  • Display the results.

Program:

Here for STM32CubeIDE to perform operation, we split the program to three parts

  1. main.c : For main function declaration and calling the function
  2. list.h : For header declaration
  3. list.c : For function definition

main.c:

#include "list.h"                                                //Calling the header file
int main()                                                       //main function starts
{
	int first_list_nodes=0, second_list_nodes = 0, first_list_items = 0, second_list_items = 0;    //Initializing the variables to zero
	int i;
	printf("Enter the number of nodes in the first list:");      //To print the number of elements
	fflush(stdout);                                              //For clearing the cache data                             
	scanf("%d",&first_list_nodes);                               //For scanning the number of elements
	printf("Enter %d the elements:",first_list_nodes);           //To tell the user to enter the elements
	for(i=0;i<first_list_nodes;i++)                              //For loop to take entered values from the console.
	{
		fflush(stdout);
		scanf("%d",&first_list_items);
		head1=insert_node(head1,first_list_items);
	}
	display_list(head1);
	printf("\n Enter the number of nodes in the second list:");  //To print enter the elements in the second list.
	fflush(stdout);                                              //clearing cache files
	scanf("%d",&second_list_nodes);                              //To scan the number of elements
	printf("Enter %d the elements",second_list_nodes);           //To tell user to enter the elements
	for(i=0;i<second_list_nodes;i++)                             //For loop to scan the values from the console
	{ 
		fflush(stdout);
	scanf("%d",&second_list_items);
	head2=insert_node(head2,second_list_items);
	}
	display_list(head2);                                         //Function called to display the linked list
	res_union = get_union(head1,head2);                          //function get_union called here and values copied to res_union
	printf("Here is the result of union:");                      //To tell the user the result is been printed next
	display_list(res_union);                                     //calling the display function to print the values in res_union
	res_intersection=get_intersection(head1,head2);              //intersection function is called here and value stored to res_intersection
	printf("Here is the result of intersection:");               //To tell the user the result of intersection is been printed next.
	display_list(res_intersection);                              //To display the result of intersection.
	return 0;                                                    //To return the value
}

list.c:

#include "list.h"                         //Header list.h is called here
void display_list(Node *head_ref)         //function definition
{
	if(head_ref == 0)                     //To check the head pointer is showing 0.
	{
		printf("List is empty");         //If yes, it prints list is empty
	}
	else
	{
		tail = head_ref;                  //Else the head pointer value is moved to tail.
		while(tail!=0)                    //Condition for tail not equal to zero.
		{
			printf("%d",tail->data);      //If while is true it prints the value in tail.data
			tail=tail->next;              //The pointer is moved to next position
		}
		tail=newnode;                     //after coming out of the while loop, tail is assigned with the value newnode.


	}

}

Node *insert_node(Node *head_ref,int n)                                //Function definition.
{
printf("\nInserting Elemenets at the beginning...\n");                 //To tell user inserting elements at the beginning.
newnode = (Node*) malloc(sizeof(Node));                                //malloc used to dynamically allocate a single large block of memory with specified size.
printf("Address of created newcode:%d\n",newnode);                     //It prints the address of newcode. 
newnode->data=n;                                                       //Here the value in the n is copied to newnode.data.
printf("Inserted value is:%d\n",newnode->data);                        //The inserted value is printed
newnode->next=head_ref;                                                //newnode.next is assigned with head_ref
head_ref=newnode;                                                      //newnode address is saved to head_ref
return head_ref;                                                       //Returning the value of head_ref.
}

Node *get_union(Node *head_pointer1,Node *head_pointer2)               //Function definition to find union
{
	while(head_pointer1 !=0)                                           //If the head pointer of 1 is not equal to zero then the while loop is executed.
	{
		head_union = insert_node(head_union, head_pointer1->data);     //here insert_node function is called and value is assigned to head union.
		head_pointer1 = head_pointer1->next;                           //The head pointer1.next is assigned to head pointer1  
 
	}
	while(head_pointer2!=0)                                            //While loop is executed only when head_pointer2 is not equal to zero.
	{
		if(!compare(head_union, head_pointer2->data))                  //head_union and head_pointer2.data are compared and cheacked it it is equal.If it is not equal the if will get executed.
			head_union=insert_node(head_union, head_pointer2->data);   //insert_node is caaled here and head_union is assigned with its value.
		head_pointer2=head_pointer2->next;                             //Adress of headpointer2.next is copied to head_pointer2.
	} 
	return head_union;                                                 //head_unioin value is returned.
}

int compare(Node *head,int data)                                      //function definitio for comparing.
{
	while(head!=NULL)                                                 //If the head is not equal to null the while statement get executed.
	{
		if(head->data==data)                                          //To check both are equal or not
			return 1;                                                 //If they are equal it returns a value 1
		head = head->next;                                            //if it is not equal, head.next is saved to head.
	}
	return 0;                                                         //To return value


}
Node *get_intersection(Node *head_pointer1, Node *head_pointer2)      //Function definition to find intersection
{
	while(head_pointer1!=0)                                           //while loop to check head_pointer is not equal to zero.
	{
		if(compare(head_pointer2, head_pointer1->data))               //If they are equal 
			head_intersection = insert_node(head_intersection, head_pointer1->data);    //insert_node is called here.
		head_pointer1=head_pointer1->next;                           //head_pointer1.next is saved to head_pointer1


	}
	return head_intersection;                                        //To return the value of intersection
}


list.h:

#ifndef LIST_H_
#define LIST_H_
#include       //Pre-processor for standarnd I/O operations
#include      //Pre-processor for library functions
#include     //Pre-processor for boolean calculations
typedef struct node     //Structure is declared of typedef to create new name for existing data-type.
{
	int data;                 //variable data of integer type declared
	struct node *next;        //Variable next with pointer declared.
}Node;                        //Name of the structure. 

static Node *head1, *head2, *head, *head_union, *head_intersection, *newnode,*tail,*res_union,*res_intersection; //Declaration of pointers of struct node type.
Node *insert_node(Node *head_ref, int n);                             //Function declaration for insertion with Node and integer data type.
void display_list(Node *head_ref);                                    //function declaration for displaying the elements of void type.
Node *get_union(Node *head_pointer1, Node *head_pointer2);            //Fucntion declaration to get union calculation with pointer in the arguments.
Node *get_intersection(Node *head_pointer1, Node *head_pointer2);     //Function declaration to get intersection of lists.
int compare(Node *head, int data);                                    //Function delcaration to comparewith arguments.

#endif /* LIST_H_ */

Output:

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.

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.