Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

/*
* pro1.c
*
* Created on: 05-Nov-2022
* Author: Priyanka
*/

//Adding standard header file

#include <stdio.h>

//Adding standard library header file for memory allocation
#include <stdlib.h>

// defining structure of linked list node
struct node

{
int data;
struct node *next;


} *listfirst, *listtwo, *listunion, *listintersection;//structure variables

//function for linked list
void initialize()

{
listfirst = listtwo = NULL;
}


//function to Inserts a node in a singly linked list.

void insert(struct node **head, int num)

{
// Create a new Linked List node
//memory allocation
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}

// Searching an element in Linked List by traversing
int search(struct node *head, int num)

{
while (head != NULL)

{
if (head->data == num)

{
return 1;
}
head = head->next;
}
return 0;

}

//Function for finding union of two given linked list

struct node* searchunion(struct node *listfirst, struct node *listtwo)

{
listunion = NULL;
// Add all nodes of first Linked List to union list
struct node *temp = listfirst;
while(temp != NULL)

{
insert(&listunion, temp->data);
temp = temp->next;
}


// Insert those nodes of listtwo which is not present in listfirst
while(listtwo != NULL)

{
if(!search(listfirst, listtwo->data))

{
insert(&listunion, listtwo->data);
}
listtwo = listtwo->next;
}
return listunion;
}

//function for getting linked list of two list

struct node *intersection(struct node *listfirst, struct node *listtwo)

{
listintersection = NULL;

//Search every element of listfirst in listtwo, If found then add it to intersection List
while(listfirst != NULL)

{
if(search(listtwo, listfirst->data))

{
insert(&listintersection, listfirst->data);
}
listfirst = listfirst->next;
}
return listintersection;
}
//Prints a linked list from head node till tail node

void listprint(struct node *nodePtr)

{
while (nodePtr != NULL)

{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("\n");

}
}

int main()

{
int i, listfirstCount, listtwoCount, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List=\n");
fflush(stdout);
scanf("%d", &listfirstCount);
printf("Enter %d elements for linked list one=\n", listfirstCount);
fflush(stdout);
for(i=0; i<listfirstCount; i++)

{
scanf("%d", &temp);
insert(&listfirst, temp);
}
printf("\n Entered elements for first linked list are=");
listprint(listfirst);

//creating second linked list
printf("\n Enter number of nodes in second Linked List=\n");
fflush(stdout);
scanf("%d", &listtwoCount);
printf("Enter %d elements for second linked list=\n", listtwoCount);
fflush(stdout);
for(i=0; i<listtwoCount; i++)

{
scanf("%d", &temp);
insert(&listtwo, temp);
}
printf("\n Entered elements in second list are=");
listprint(listtwo);
//printing Union of two given linked list
searchunion(listfirst, listtwo);
intersection(listfirst, listtwo);
printf("\n Union of two Linked List=\n");
fflush(stdout);
listprint(listunion);
printf("\n Intersection of two Linked List=\n");
fflush(stdout);
listprint(listintersection);
return 0;
}

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.