Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
#include
typedef struct node
{
int data;
struct node *next;
}
Node;
static Node *head1, *head2, *head, *head_union, *head_intersection, *newnode, *tail, *res_union, *res_intersection;
Node *insert_node(Node *head_ref, intn);
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);
void display_list(Node *head_ref)
{
if(head_ref == 0)
{
printf("list is empty.");
}
else
{
tail = head_ref;
while(tail != 0)
{
printf("%d", tail->data);
tail = tail->next;
}
tail = newnode;
}
}
Node *insert_node(Node *head_ref, int n)
{
printf("\n Inserting elements at the beginning....\n");
newnode =(Node*) malloc(size of (Node));
printf("Addres of created newnode:%d\n",newnode);
newnode->data = n;
printf("Inserted value is :%d\n", newnode->data);
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_pointer->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_insection(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer != 0)
{
if(compare(head_pointer2, head_pointer1->data))
head intersection = insert_node(head_intersection, head_pointer->data);
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}
int main()
{
int first_list_nodes = o,second_list_nodes = 0, first _list_items = 0;
second_list_items = 0;
int f;
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( f == 0; f <first _list_nodes;f++)
{
fflush(stdout);
scanf("%d" ,& first_list_items);
head1 = insert_node(head1, first_list_items);
}
display_list(head1);
printf("\nEnter the number of Nodes in second list :");
fflush(stdout);
scanf("%d" ,&second_list_nodes);
printf("Enter %d the elements :",second_list_nodes);
for( f = 0; f <second_list_nodes;f++)
{
fflush(stdout)
scanf("%d" ,&second_list_items);
head2 = insert_node(head2,second_list_items);
}
display_list(head2);
res_union = get_union(head1,head2);
printf("Here is the result of union :");
display_list(res_union);
res_intersection = get_intersection(head1,head2);
printf("Here is the result of intersection:");
display_list(res_intersection);
}
Output
Enter the number of Nodes in first list : 4
Enter 4 the elements : 1 2 3 4
Inserting elements at the beginning...
Address of created newnode :-22016064
Inserted value is : 1
Inserting elements at the beginning...
Address of created newnode : -22016032
Inserted value is : 2
Inserting elements at the beginning...
Address of created newnode : -22016000
Inserted value is : 3
Inserting elements at the beginning...
Address of created newnode : -22015968
Inserted value is : 4
Enter the number of Nodes in second list : 4
Enter 4 the elements : 4 3 2 1
Inserting elements at the beginning...
Address of created newnode : -22015808
Inserted value is : 4
Inserting elements at the beginning...
Address of created newnode : -22015776
Inserted value is : 3
Inserting elements at the beginning...
Address of created newnode : -22015744
Inserted value is : 2
Inserting elements at the beginning...
Address of created newnode : -22015712
Inserted value is : 1
Inserting elements at the beginning...
Address of created newnode : -22015600
Inserted value is : 5
Here in the result of union : 5 1 2 3 4
Inserting elements at the beginning...
Address of created newnode : -22015640
Inserted value is : 4
Inserting elements at the beginning...
Address of created newnode : -22015616
Inserted value is : 3
Inserting elements at the beginning...
Address of created newnode : -22015584
Inserted value is : 2
Here is the result of intersection : 2 3 4