Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
PROJECT:1 Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
PROGRAM:
#include // Pre Processor Directive with Standard i/p and o/p header file //
#include // Pre Processor Directive with Standard library header file //
#include // Pre Processor Directive with Standard boolean header file //
//-----Define the Node-----//
typedef struct node // Structure is used to define Node
{
int data; // int is a data type
struct node *next; // *pointing the address
}Node;
static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;
//-----Define the functions-----//
Node *insert_node(Node *head_ref,int n); // Calling the insert_node
void display_list(Node *head_ref); // Displaying
Node *get_union(Node *head_pointer1,Node *head_pointer2); // calling the union
Node *get_intersection(Node *head_pointer1,Node *head_pointer2); // calling the intersection
int compare(Node *head,int data); // calling the compare
int main() // Main Funtion
{
int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,second_list_items = 0;
/* INSERTING NODES AND DISPLAYING */
/* FIRST LIST */
printf("Enter the number of Nodes in first List:"); // Getting the number of nodes from User for first list
fflush(stdout); // Flushing the previous error
scanf("%d",&first_list_nodes); // %d format specifier, & to address were want to scan
printf("Enter %d the elements:",first_list_nodes); // Displaying on screen, Getting elements from User
for(int i = 0;i < first_list_nodes;i++) /* for loop used the condition is first_list_node is l lesser then i then only it goes inside a loop orelse it exits the loop */
{
fflush(stdout); // Flushing the previous error
scanf("%d",&first_list_items); // Getting first_list_items
head1 = insert_node(head1,first_list_items); // head1 is Variable name for first list
}
display_list(head1); // Displaying whatever in head1
/*SECOND LIST*/
printf("\nEnter the number of Nodes in second List:"); // Getting the number of nodes from User for second list
fflush(stdout); // Flushing the previous error
scanf("%d",&second_list_nodes); // %d format specifier, & to address were want to scan
printf("Enter %d the elements:",second_list_nodes); // Displaying on screen, Getting the elements from User
for(int i = 0;i < second_list_nodes;i++) /* for loop used the condition is second_list_node is lesser then i then only it goes inside a loop orelse it exits the loop */
{
fflush(stdout);
scanf("%d",&second_list_items); // Getting first_list_items
head2 = insert_node(head2,second_list_items); // head2 is Variable name for second list
}
display_list(head2); // Displaying whatever in head2
/* RESULT OF UNION */ // Union operation between head1 and head2.Put inside the variable res_union
res_union = get_union(head1,head2); // res_union as variable name
printf(" Here is the result of union: "); // Displaying the results of union on screen
display_list(res_union); // Displaying res_union
/* RESULT OF INTERSECTION */
res_intersection = get_intersection(head1,head2); // res_intersection as variable name
printf(" Here is the result of intersection: "); // Displaying the results of intersection on screen
display_list(res_intersection); // Displaying res_intersection
}
/*---DISPLAY---*/
void display_list(Node *head_ref)
{
if(head_ref == 0) // head_ref is equal to 0. It goes inside the bracket and prints the statement
{
printf(" List is empty.");
}
else // else head_ref pushes the value into tail
{
tail = head_ref; // Passing values of head_ref to tail
while(tail != 0) // tail should not equal to 0 then only it goes inside bracket otherwise it goes outside
{
printf("%d ", tail->data); // prints the data of current node
tail = tail->next; // Process will be repeated
}
tail = newnode;
}
}
/*--- INSERT NODE---*/ // Holds the values of him and address of next node //
Node *insert_node(Node *head_ref,int n)
{
printf("\nInserting Elements at the beginning...\n"); // Getting the elements from user and inserting in newnode
newnode =(Node*) malloc(sizeof(Node)); // malloc means giving intimation to preprocessor that occupies less space
printf("Address of created newnode :%d\n",newnode); // we get the address of an element from newnode and prints on screen
newnode->data = n;
printf("Inserted value is :%d\n",newnode->data); // Inserted value is displayed on screen
newnode->next = head_ref; // Process will be repeated
head_ref = newnode;
return head_ref;
}
/*---UNION---*/ // Having all values
Node *get_union(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1 != 0) // Checking not equal to 0
{
head_union = insert_node(head_union,head_pointer1->data); // Pushes the value of head_pointer1 into head_union
head_pointer1 = head_pointer1->next; // For repeating the process
}
while(head_pointer2 != 0) // Checking not equal to 0
{
if(!compare(head_union,head_pointer2->data)) // Comparing head_pointer2 with head_union
head_union = insert_node(head_union,head_pointer2->data); // head_union is a variabble name and stores the value of union
head_pointer2 = head_pointer2->next; // Process will be repeated
}
return head_union;
}
/*---COMPARE---*/
int compare(Node *head,int data)
{
while (head != NULL) // head values not should be NULL
{
if (head->data == data) // if both data is not equal means it goes to next data
return 1;
head = head->next; // Process will be repeated
}
return 0;
}
/*---INTERSECTION---*/
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while (head_pointer1 != 0) // we have to check it is equal to 0
{
if (compare(head_pointer2, head_pointer1->data)) // comparing both head_pointer1 and 2 and pointer2 is kept constant
head_intersection = insert_node(head_intersection,head_pointer1->data); // pushing head_pointer1 data into head intersection
head_pointer1 = head_pointer1->next; // Process will be repeated
}
return head_intersection;
}
OUTPUT:
Enter the number of Nodes in first List:5
Enter 5 the elements:2 3 4 5 6
Inserting Elements at the beginning...
Address of created newnode :581130944
Inserted value is :2
Inserting Elements at the beginning...
Address of created newnode :581130976
Inserted value is :3
Inserting Elements at the beginning...
Address of created newnode :581131008
Inserted value is :4
Inserting Elements at the beginning...
Address of created newnode :581131040
Inserted value is :5
Inserting Elements at the beginning...
Address of created newnode :581131072
Inserted value is :6
6 5 4 3 2
Enter the number of Nodes in second List:5
Enter 5 the elements:3 4 5 6 7
Inserting Elements at the beginning...
Address of created newnode :581131104
Inserted value is :3
Inserting Elements at the beginning...
Address of created newnode :581131136
Inserted value is :4
Inserting Elements at the beginning...
Address of created newnode :581131168
Inserted value is :5
Inserting Elements at the beginning...
Address of created newnode :581131200
Inserted value is :6
Inserting Elements at the beginning...
Address of created newnode :581131232
Inserted value is :7
7 6 5 4 3
Inserting Elements at the beginning...
Address of created newnode :581131264
Inserted value is :6
Inserting Elements at the beginning...
Address of created newnode :581131296
Inserted value is :5
Inserting Elements at the beginning...
Address of created newnode :581131328
Inserted value is :4
Inserting Elements at the beginning...
Address of created newnode :581131360
Inserted value is :3
Inserting Elements at the beginning...
Address of created newnode :581131392
Inserted value is :2
Inserting Elements at the beginning...
Address of created newnode :581131424
Inserted value is :7
Here is the result of union: 7 2 3 4 5 6
Inserting Elements at the beginning...
Address of created newnode :581131456
Inserted value is :6
Inserting Elements at the beginning...
Address of created newnode :581131488
Inserted value is :5
Inserting Elements at the beginning...
Address of created newnode :581131520
Inserted value is :4
Inserting Elements at the beginning...
Address of created newnode :581131552
Inserted value is :3
Here is the result of intersection: 3 4 5 6
SCREENSHOTS:







