Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stdio.h>
#include <stdlib.h>
/* Linked list node */
typedef struct Node
{
int data;
struct Node* next;
}
Node;
/* Function to create a
new node with given data */
Node* newNode(int data)
{
Node* new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
/* Function to insert a node at the
beginning of the Singly Linked List */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = newNode(new_data);
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// A utility function to print a linked list
void printList(Node* node)
{
while (node != NULL)
{
printf("%d ",node->data);
node = node->next;
}
printf("\n");
}
int main(void)
{
Node* first = NULL;
Node* second = NULL;
// create first list 1->2->3->4->5
push(&first, 5);
push(&first, 4);
push(&first, 3);
push(&first, 2);
push(&first, 1);
printf("First List is = ");
printList(first);
// create second list 3->4->5
push(&second, 5);
push(&second, 4);
push(&second, 2);
printf("Second List is = ");
printList(second);
struct Node *inter1=first;
struct Node *inter2=second;
printf("\n INTERSECTION OF TWO LIST ");
while(inter1 !=NULL )
{
while(inter2 !=NULL )
{
if(inter1->data==inter2->data)
{
printf("\n %d",inter2->data);
}
inter2=inter2->next;
}
inter1=inter1->next;
inter2=second;
}
struct Node *UNION1=first;
struct Node *UNION2=second;
printf("\n UNION OF TWO LIST ");
int x;
while(UNION1 !=NULL )
{
while(UNION2 !=NULL )
{
if(UNION1->data!=UNION2->data )
{
x=1;
}
if (UNION1->data==UNION2->data)
{
x=0;
break;
}
UNION2=UNION2->next;
}
if(x==1)
{
printf("\n%d",UNION1->data);
}
UNION1=UNION1->next;
if(x==0)
{
printf("\n%d",UNION2->data);
}
}
}
