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)

\* LIST.H *\

#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;


static Node *head1,*head2,*head,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;


// define the functions

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);

#endif /* LIST_H_ */

/* LIST.C */

# include"list.h"

void display_list(Node *head_ref)
{
#ifdef DEBUG
printf("entering display function\n");
#endif
if(head_ref == 0)
{
printf(" list is empty.");
}
else
tail = head_ref;
while(tail !=0)
{
printf("%d ",tail->data); // prints the data of currents node
tail = tail->next; // advanced 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 the beginning...\n");
newnode =(Node*) malloc(sizeof(Node));
printf("address of created newnode : %d\n",newnode);
newnode->data = n;
printf("Inserted value is :%d\n",newnode->data);
newnode->next = head_ref;
head_ref = newnode;
#ifdef DEBUG
printf("head address: %d\n",head_ref);
printf("newnode address: %d\n",newnode);
printf("exiting insert function\n");
#endif
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;
}

/* MAIN.C */

#include "list.h"

int main()
{
int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,
second_list_items = 0;
printf("enter the number of Nodes in first list:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("enter %d the elements:",first_list_nodes);
for(int i = 0;i < first_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&first_list_items);
head1 = insert_node(head1,first_list_items);
}

display_list(head1);
printf("\nEnter the number of Nodes in seconds list:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("enter %d the elements:",second_list_nodes);
for(int i = 0;i < second_list_nodes;i++)
{
fflush(stdout);
scanf("%d",&second_list_items);
head2 = insert_node(head2,second_list_items);
}
display_list(head2);
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("value 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);
#ifdef DEBUG_MAIN
printf("\nValue of head1 and head2 after performing union : %d %d\n",head1,head2);
printf("value of res_intersection : %d\n",res_intersection);
#endif
printf("Here is the result of intersection: ");
display_list(res_intersection);

}

OUTPUT:

enter the number of Nodes in first list:4 5 6 7 8
enter 4 the elements:
Inserting elements the beginning...
address of created newnode : 7552976
Inserted value is :5

Inserting elements the beginning...
address of created newnode : 7543848
Inserted value is :6

Inserting elements the beginning...
address of created newnode : 7543864
Inserted value is :7

Inserting elements the beginning...
address of created newnode : 7542376
Inserted value is :8
8 7 6 5
Enter the number of Nodes in seconds list:2 3 4 9 1
enter 2 the elements:
Inserting elements the beginning...
address of created newnode : 7542392
Inserted value is :3

Inserting elements the beginning...
address of created newnode : 7542408
Inserted value is :4
4 3
Inserting elements the beginning...
address of created newnode : 7542424
Inserted value is :8

Inserting elements the beginning...
address of created newnode : 7542440
Inserted value is :7

Inserting elements the beginning...
address of created newnode : 7542456
Inserted value is :6

Inserting elements the beginning...
address of created newnode : 7542472
Inserted value is :5

Inserting elements the beginning...
address of created newnode : 7542488
Inserted value is :4

Inserting elements the beginning...
address of created newnode : 7542504
Inserted value is :3
here is the result of union:3 4 5 6 7 8 Here is the result of intersection: list is empty.3 4 5 6 7 8

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.