Challenge

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

Live Batch (Preeti):

Q:

/*
* ProjectQues.c
*
* Author: Sankalp Tendolkar
*/

#include<stdio.h>
#include<stdlib.h>

//* A structure of linked list node *//
struct node {
int data;
struct node *next;
} *LinkedListOne, *LinkedListTwo, *unionLinkedList, *intersectionLinkedList;

void initialize(){
LinkedListOne = LinkedListTwo = NULL;
}
//*
Given a Inserts a node in front of a singly linked list.
*//
void insert(struct node **head, int num) {
//* Create a new Linked List node *//
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;
}

//* Searches an element in Linked List by
linearly traversing from head to tail *//
int search(struct node *head, int num) {
while (head != NULL) {
if (head->data == num){
return 1;
}
head = head->next;
}
return 0;
}

//*
Returns the union of two given linked list
*//
struct node* findunion(struct node *LinkedListOne, struct node *LinkedListTwo) {
unionLinkedList = NULL;
//* Add all nodes of first Linked List to unionLinkedList *//
struct node *temp = LinkedListOne;
while(temp != NULL){
insert(&unionLinkedList, temp->data);
temp = temp->next;
}
//* Insert those nodes of LinkedListTwo which is not present in LinkedListOne *//
while(LinkedListTwo != NULL){
if(!search(LinkedListOne, LinkedListTwo->data)){
insert(&unionLinkedList, LinkedListTwo->data);
}
LinkedListTwo = LinkedListTwo->next;
}
return unionLinkedList;
}
//*
Returns the Linked List which contains common nodes of two given linked list
*//
struct node* intersection(struct node *LinkedListOne, struct node *LinkedListTwo) {
intersectionLinkedList = NULL;
//* Search every element of LinkedListOne in LinkedListTwo,
If found then add it to intersection List *//
while(LinkedListOne != NULL){
if(search(LinkedListTwo, LinkedListOne->data)){
insert(&intersectionLinkedList, LinkedListOne->data);
}
LinkedListOne = LinkedListOne->next;
}
return intersectionLinkedList;
}
//*
Prints a linked list from head node till tail node
*//
void printLinkedList(struct node *nodePtr) {
while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}

int main() {
int i, LinkedListOneCount, LinkedListTwoCount, temp;
initialize();
//* Creating First linked List*//
printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d", &LinkedListOneCount);
printf("Enter %d integers\n", LinkedListOneCount);
fflush(stdout);
for(i=0; i<LinkedListOneCount; i++){
scanf("%d", &temp);
insert(&LinkedListOne, temp);
}
printLinkedList(LinkedListOne);
printf("\nEnter number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d", &LinkedListTwoCount);
printf("Enter %d integers\n", LinkedListTwoCount);
fflush(stdout);
for(i=0; i<LinkedListTwoCount; i++){
scanf("%d", &temp);
insert(&LinkedListTwo, temp);
}
printLinkedList(LinkedListTwo);
//* printing Union of two given linked list *//
findunion(LinkedListOne, LinkedListTwo);
intersection(LinkedListOne, LinkedListTwo);
printf("\nUnion Linked List\n");
fflush(stdout);
printLinkedList(unionLinkedList);
printf("\nIntersection Linked List\n");
fflush(stdout);
printLinkedList(intersectionLinkedList);

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.