Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
Overview of the project:
In this project, interfaces for mathematical set theory are implement using c programming language. user gives the input like two list of data and its program will give the outputas the unionand intersection of these two lists.
1.here we taken two set of the data A=[1,2,3,4], and B[2,3,4,5,].
2.After that we can apply the union and intersection of these data A,B.
Goals
1.Implementation of interface for mathematical set theory using c programming.
2. Giving two list of data as input to get the output as union & intersection for Given list of data.
3.union means it will store the all data in same location.
Code:
/*
==================================================================
Name : project1.c
Author :Monika
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
==================================================================
*/
#include "list.h" //header//
int main() //main function//
{
int first_list_node=0,second_list_node=0,first_list_item=0,second_list_item=0; //local variable declaration//
printf("\n enter no of nodes in first list:"); //displayed the message to the user to enter the first list//
//asked to user how many numbers in list he is going to enter//
fflush(stdout);
scanf("%d",&first_list_node); //read the list items of first list//
printf("\n enter %d the elements:",first_list_node);
for (int i=0;i<first_list_node;i++) //for loop for the scanning different values//
{
fflush(stdout); //cleared cache memory//
scanf("%d",&first_list_item); //read the first list item//
head1=insert_node(head1,first_list_item);
}
display_list(head1); //display list head 1//
printf("\n enter no of nodes in second list:"); //asked the user to enter the second list//
fflush(stdout); //cleared cache memory//
scanf("%d",&second_list_node); //read the list items of second list//
printf("\n enter %d the elements:",second_list_node);
for (int i=0;i<second_list_node;i++) //for loop to scan the second list//
{
fflush(stdout); //cleared cache memory//
scanf("%d",&second_list_item); //read the second list item//
head2=insert_node(head2,second_list_item);
}
display_list(head2); //display list head 2
res_union=get_union(head1,head2);
#ifdef DEBUG MAIN
printf("\nvalue of head1 and head2:%d%d",head1,head2);
#endif
#ifdef DEBUG MAIN
printf("\nvalue of head1 and head2 after performing union:%d%d\n",head1,head2);
printf("\nvalue of res_union:%d%\n",res_union);
#endif
printf("here is the result of union:");
display_list(res_union);
res_intersection=get_intersection(head1,head2); //re-enter the section ofhead 1 and head2//
#ifdef DEBUG MAIN
printf("\nvalue of head1 and head2 after performing intersection:%d%d\n",head1,head2);
printf("\nvalue of res_intersection:%d%\n",res_intersection);
#endif
printf("\nhere is the result of intersection:"); //display the message to the user result of intersection before displaying the output//
display_list(res_intersection); // call the intersection to compare list//
/*......................................*/
* list.c
*
* Created on: Apr 26, 2022
* Author: PC
*.......................................*/
#include "list.h"
void display_list(Node *head_ref)
{
if(head_ref == 0) //if statement if user does not enters any number it will showthe message list is empty//
{
printf("list is empty");
}
else
{
tail=head_ref;
while(tail !=0)
{
printf("%d",tail->data); //print the data of current node//
tail=tail->next; //advances the position of current node//
}
tail=newnode;
#ifdef DEBUG
printf("value of tail is :%d\n",tail);
#endif
}
}
Node *insert_node(Node *head_ref,int n)
{
printf("\ninserting elements at the beginning....\n"); //insert the message to the user//
newnode=(Node*)malloc(sizeof(Node));
printf("address of created newnode:%d \n",newnode); //display the address of the first items//
newnode->data = n;
printf("inserted value is:%d\n", newnode->data); //display the values of the lisy 1 item//
newnode->next=head_ref;
head_ref=newnode;
#ifdef DEBUG
printf("head address:%d",head_ref);
printf("new node address:%d",newnode);
printf("existing insert function\n");
#endif
return head_ref;
}
Node *get_union(Node *head_pointer1, Node *head_pointer2)
{
while(head_pointer1 !=0)//check whether the head pointer not equal to 0//
{
head_union=insert_node(head_union,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
while(head_pointer2 !=0)
{
if (!compare(head_pointer2, head_pointer2->data))
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next;
}
return head_union;
}
int compare(Node *head, int data)
{
while(head !=NULL)
{
if(head->data==data)
return 1;
head=head->next; //the head less than next data//
}
return 0;
}
Node *get_intersection(Node *head_pointer1,Node*head_pointer2) //get the intersection output of the both//
{
while(head_pointer1 !=0) //check whether the head pointer is not equal to 0//
{
if (compare(head_pointer2, head_pointer1->data))
head_intersection=insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
return head_intersection;
}
}
/*............................*/
* list.h
*
* Created on: Apr 26, 2022
* Author: PC
*..................................*/
#ifndef LIST_H_
#define LIST_H_
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
/*....define the node....*/
typedef struct node
{
int data;
struct node *next;
}Node;//user define data typedef declaration data and node//
static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;//static variable declaration//
/*....define the function...*/
Node *insert_node(Node *head_ref,int n);//head ref data//
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);//compare the values of both the list//
#endif /* LIST_H_ */
Expected Outcomes: here when we run the code we got output in console
enter no of nodes in first list:4
enter 4 the elements:1 2 3 4
inserting elements at the beginning....
address of created newnode:12196928
inserted value is:1
inserting elements at the beginning....
address of created newnode:12196944
inserted value is:2
inserting elements at the beginning....
address of created newnode:12196960
inserted value is:3
inserting elements at the beginning....
address of created newnode:12196976
inserted value is:4
4321
enter no of nodes in second list:4
enter 4 the elements:2 3 4 5
inserting elements at the beginning....
address of created newnode:12197072
inserted value is:2
inserting elements at the beginning....
address of created newnode:12197088
inserted value is:3
inserting elements at the beginning....
address of created newnode:12197104
inserted value is:4
inserting elements at the beginning....
address of created newnode:12197120
inserted value is:5
5432
inserting elements at the beginning....
address of created newnode:12197136
inserted value is:4
inserting elements at the beginning....
address of created newnode:12197152
inserted value is:3
inserting elements at the beginning....
address of created newnode:12195448
inserted value is:2
inserting elements at the beginning....
address of created newnode:12195464
inserted value is:1
here is the result of union:1234
inserting elements at the beginning....
address of created newnode:12195480
inserted value is:4
inserting elements at the beginning....
address of created newnode:12195496
inserted value is:3
inserting elements at the beginning....
address of created newnode:12195512
inserted value is:2
here is the result of intersection:234