Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
Q-1
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists.
Program:
#include "L_list.h"
int main(){
int first_listData=0,secound_ListData=0,first_listitem=0,secound_listitem=0,i;
printf("Please Enter Number of element for 1st List :\n");
fflush(stdout);
scanf("%d",&first_listData);
printf("Enter %d of elements:\n",first_listData);
for(i=0;i<first_listData;i++){
fflush(stdout);
scanf("%d",&first_listitem);
head1 = insert_node(head1,first_listitem);
}
display_list(head1);
printf("\n Please Enter Number of element for 2st List :\n");
fflush(stdout);
scanf("%d",&secound_ListData);
printf("Enter %d of elements:\n",secound_ListData);
for(i=0;i<secound_ListData;i++){
fflush(stdout);
scanf("%d",&secound_listitem);
head2=insert_node(head2,secound_listitem);
}
display_list(head2);
res_union=get_union(head1,head2);
printf("\n Union Are: ");
display_list(res_union);
res_intersection= get_intersection(head1,head2);
printf("\n Intersection Are: ");
display_list(res_intersection);
return 0;
}
//header File
#ifndef L_LIST_H_
#define L_LIST_H_
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node;
static Node *tail,*head1,*head2,*head,*head_union,*head_intersection,*newnode,*res_union,*res_intersection;
Node *insert_node(Node *head_ref,int n);
void display_list(Node *head_ref);
Node *get_union(Node *head_ptr1,Node *head_ptr2);
Node *get_intersection(Node *head_ptr1,Node *head_ptr2);
int compare(Node *head,int data);
#endif /* L_LIST_H_ */
//C source File:
#include "L_list.h"
void display_list(Node *head_ref){
if (head_ref==0){
printf("List Is Empty !!\n");
}
else
{
tail=head_ref;
while (tail != 0)
{
printf(" %d",tail->data);
tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n)
{
newnode=(Node*)malloc(sizeof(Node));
newnode->data=n;
newnode->next= head_ref;
head_ref=newnode;
return head_ref;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2){
while(head_pointer1 !=0){
head_union= insert_node(head_union,head_pointer1->data);
head_pointer1= head_pointer1->next;
}
while(head_pointer2 !=0){
if(!compare(head_union,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;
}
return 0;
}
Node *get_intersection(Node *head_pointer1,Node *head_pointer2){
while(head_pointer1 !=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;
}
Result in Console:
