Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node //link list node
{
int data;
struct node *next;
};
void push(struct node** head_ref, int new_data); //function declaration
int present(struct node* head,int data);
struct node* getunion(struct node* head1,struct node* head2) //getunion two link list head1,head2
{
struct node* result = NULL;
struct node *t1 = head1, *t2 = head2;
while(t1 != NULL) //insert list1 to the result list
{
push(&result, t1->data);
t1 = t1->next;
}
while(t2 != NULL) //insert list2 are not present the result list
{
if(! present(result, t2->data))
{
push(&result, t2->data);
}
t2 = t2->next;
}
return result;
}
struct node *getintersection(struct node *head1,struct node *head2) //getintersection two link list head1,head2
{
struct node* result = NULL;
struct node* t1 = head1;
while(t1 != NULL) // list1 values each of list2 is same insert to the result
{
if(present(head2, t1->data))
{
push(&result, t1->data);
}
t1 = t1->next;
}
return result;
}
void push(struct node** head_ref , int new_data) //insert node on beginning of the link list
{
struct node* new_node = (struct node*) malloc(sizeof(struct node)); // node
new_node->data = new_data; //data
new_node->next = (*head_ref); //old list off new node
(*head_ref) = new_node; // head to the new node
}
void printlist(struct node* node) //print a link list
{
while(node != NULL)
{
printf("%d", node->data);
node = node->next;
}
}
int present(struct node* head,int data) // function return true,data is present link list else return false
{
struct node* t = head;
while(t != NULL)
{
if(t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
int main() //program test a function
{
int a,b,temp;
printf("enter the value of number of value in link list (both):-");
fflush(stdout);
scanf("%d %d", &a, &b);
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* intersec = NULL;
struct node* uni = NULL;
for(int i=0;i<a;i++) //link list1 for loop
{
printf("enter the value of link list1 is %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp);
}
for(int i=0;i<b;i++) //link list2 for loop
{
printf("enter the value of link list2 is %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp);
}
intersec = getintersection(head1, head2); //function to intersection
uni = getunion(head1, head2); //function to union
printf("\n first list is");
printlist(head1);
printf("\n second list is");
printlist(head2);
printf("\n intersection list is");
printlist(intersec);
printf("\n union list is");
printlist(uni);
return 0;
}