Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
code//
/*
**********************Project1************************
*
* Created on: 03-Jul-2023
* Author: GOURAV SANKADIA
* EMAIL:[email protected]
* Contact:9933283512
*/
#include <stdio.h>
#include <stdlib.h>
/* A structure of linked list node, node contain two parts one is data and other one is link part */
struct node {
int data;//data to be stored
struct node *next;//lint for the next node
} *list1, *list2, *unionlist, *intersectionlist;//list1: linked list 1 which is rtaken as input
//list2:linked list 2
//unionlist:list of unions among two list
//list initializer//intersection:intersection of two lists
void initialize(){
list1 = list2 = NULL;
}
/*
Given a Inserts a node in front of a singly linked list.
*/
void createlist(struct node **head, int num)
{
/* Create a new Linked List node */
struct node* new = (struct node*) malloc(sizeof(struct node));
new->data = num;//num is data stored in first block of node
/* Next pointer of new node will point to head node of linked list */
new->next = *head;//head is the initial address part which is giving a link of address of first node the first node give the address of next node
/* make new node as new head of linked list */
*head = new;
}
/* Searches an element in Linked List by
linearly traversing from head to tail */
int searchelemnt(struct node *head, int num)//derch elemnt function traverse from head to tail and we use this when we perform union or intaersevtion operation
{
while (head != NULL) //when head is not null means it is having some address
{
if (head->data == num)//and the address of head pointing to a node which contains data:num
{
return 1;//then return to 1
}
head = head->next;//now head part goes to next link means first node address part acts as link to address of next node
}
return 0;
}
/*
Returns the union of two given linked list
*/
struct node* findunion(struct node *list1, struct node *list2)
{
unionlist = NULL;//initially union list will be null
/* Add all nodes of first Linked List to unionLL */
struct node *temp =list1;
while(temp != NULL)
{
createlist(&unionlist, temp->data);//here linked list 1 data stored in union linked list one by one
temp = temp->next;
}
/* Insert those nodes of linked list 2which is not present in linked list 1 */
while(list2 != NULL)
{
if(!searchelemnt(list1, list2->data))//now
{
createlist(&unionlist, list2->data);
}
list2 = list2->next;
}
return unionlist;
}
/*
Returns the Linked List which contains common nodes of two given linked list
*/
struct node* intersection(struct node *list1, struct node *list2) {
intersectionlist = NULL;
/* Search every element of linked list in linked list 2,
If found then add it to intersection List */
while(list1 != NULL){
if(searchelemnt(list2, list1->data)){
createlist(&intersectionlist, list1->data);
}
list1 = list1->next;
}
return intersectionlist;
}
/*
Prints a linked list from head node till tail node
*/
void display(struct node *ptr)
{
while (ptr != NULL) {
printf("%d", ptr->data);
ptr = ptr->next;
if(ptr != NULL)
printf("-->");
}
}
int main() {
int i, list1Count, list2Count, temp;
initialize();
/* Creating First linked List*/
printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d", &list1Count);
printf("Enter %d integers\n", list1Count);
for(i=0; i<list1Count; i++)
{
fflush(stdout);
scanf("%d", &temp);
createlist(&list1, temp);
}
display(list1);
//create linked list 2
printf("\nEnter number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d", &list2Count);
printf("Enter %d integers\n", list2Count);
for(i=0; i<list2Count; i++)
{
fflush(stdout);
scanf("%d", &temp);
createlist(&list2, temp);
}
display(list2);
/* printing Union of two given linked list */
findunion(list1, list2);
intersection(list1, list2);
printf("\nUnion Linked List\n");
display(unionlist);
printf("\nIntersection Linked List\n");
display(intersectionlist);
return 0;
}
output:
Enter number of nodes in first Linked List
3
Enter 3 integers
1
2
3
3-->2-->1
Enter number of nodes in second Linked List
3
Enter 3 integers
4
5
3
3-->5-->4
Union Linked List
4-->5-->1-->2-->3
Intersection Linked List
3