Challenge

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.

Aim:

To Create the user defined data type to implement the user interfaces for working with 'SET' its a concept of Mathematical Set Theory to using the Linked List.

SET - A Set is a collection of objects (called members or elements) that is regarded as being a single object. To indicate that an object x is a member of a set A one writes x ∊ A, while x ∉ A indicates that x is not a member of A, In this set operation can be used in Union and Intersection.

LINKED LIST - Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. They include a series of connected nodes. Here, each node stores the data and the address of the next node.

In this project we are used in some of Linked List They are,

  • FirstLinkedList(FirstLL)
  • secondLinkedList(SecondLL)
  • UnionLinkedList(UnionLL)
  • IntersectLinkedList(IntersectLL).

Code:

#include <stdio.h>

#include <stdlib.h> //header file

// A structure of linked list node

struct node

{

int data;

struct node *next;

} *FirstLL, *SecondLL, *unionLL, *IntersectLL; //Initiate the linked Lists

void initialize()

{

FirstLL = SecondLL = NULL; //initially the value give an 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 *FirstLL, struct node *SecondLL)

{

unionLL = NULL;

// Add all nodes of first Linked List to unionLL

struct node *temp = FirstLL;

while(temp != NULL)

{

insert(&unionLL, temp->data);

temp = temp->next;

}

// Insert those nodes of SecondLL which is not present in FirstLL

while(SecondLL != NULL)

{

if(!search(FirstLL, SecondLL->data))

{

insert(&unionLL, SecondLL->data);

}

SecondLL = SecondLL->next;

}

return unionLL;

}

//Returns the Linked List which contains common nodes of two given linked list

struct node* intersection(struct node *FirstLL, struct node *SecondLL)

{

IntersectLL = NULL;

//Search every element of FirstLL in SecondLL, If found then add it to intersection List

while(FirstLL != NULL)

{

if(search(SecondLL, FirstLL->data))

{

insert(&IntersectLL, FirstLL->data);

}

FirstLL = FirstLL->next;

}

return IntersectLL;

}

//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, FirstLLCount, SecondLLCount, temp;

//Creating First linked List

printf("Enter number of Nodes in First Linked List:\n");

scanf("%d", &FirstLLCount);

printf("Enter %d integers:\n", FirstLLCount);

for(i=0; i<FirstLLCount; i++)

{

scanf("%d", &temp);

insert(&FirstLL, temp);

}

printLinkedList(FirstLL);

printf("\nEnter number of Nodes in Second Linked List:\n");

scanf("%d", &SecondLLCount);

printf("Enter %d integers\n", SecondLLCount);

for(i=0; i<SecondLLCount; i++)

{

scanf("%d", &temp);

insert(&SecondLL, temp);

}

printLinkedList(SecondLL);

//printing Union of two given linked list

findunion(FirstLL, SecondLL);

intersection(FirstLL, SecondLL);

printf("\n\nUnion Linked List\n");

printLinkedList(unionLL);

printf("\n\nIntersection Linked List\n");

printLinkedList(IntersectLL);

return 0;

}

Logic:

The Required concept of this project is to make the Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

above writing code is including all the concept is adding.

Screenshot & Ouput:

Output:

Result:

The required concepts will sucessfully implemented with this project.

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.