Challenge

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

Overview of the Project:-

  • In this Project we are going to implement the Mathematical Set Theory by using the two sets of data list and getting the output in Union and Intersection of two sets.
  • Set Theory means is a Branch of Mathematical Logic where we learn about the sets.
  • Intersection of Two Sets means we are going to get the output when the common elements are present in two sets.
  • Here we are using the Linked List concept for to initilaze the two sets of numbers or elements.
  • Getting the output in the union form.

Goals:-

  • We need to implement the 2 sets by the using the linked list to perform Mathematical Set Operation.
  • We need to use push function,print function ,union function, intersection function and we need to compare the 2 sets and get the output only the same elements in the 2 sets.

Key Highlights of the Project:-

  • By using the Singly Linked list concept we are implementing the two sets in c-Programming.
  • First we need to enter the set 1 number of nodes and read the values and again we need to give the set 2 number of nodes and then reads the values.
  • The values will store in the node in Head Pointer(Starting Address of list),Tail Pointer(Temporary Pointer used for tranversing in the list) and the address of next node(Points the new created node).
  • After giving the 2 sets. we need to read the set by last in first out process and print the 2 sets.
  • We want to Comparing the two sets and find out the common elements in the sets.
  • From Union we are going to get only the common elements in the set.

Deliverables of this Project:

  • Create the input for number of nodes in the 1st set.
  • Take multiple inputs for the data on nodes in 1st set and insert the nodes in the first set using push() function. Loop is used for multiple iteration.
  • Call print() function to display 1st set.
  • Repeat above steps for the 2nd set.
  • Need to save the head pointer address return to the union() function pointer to a Node type pointer result - union.
  • Pass the address of result - union to the print() function and display the union list.

Expected Outcomes:-

  • By using the Linked List we can creat the set of elements.
  • By getting the 2 sets we need to store the outputs in the union.
  • We need to compare the common number/elements in the set by using intersection concept.

Program coding:-

#include //standard i/o header file
#include //The header defines variable types,
//several macros, and various functions for performing general functions.

// Link list node
struct Node
{
int value;
struct Node* next;
};

/*function to insert a node at the beginning of a linked list */
void push(struct Node** head_ref, int new_value)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // allocate node */
new_node->value = new_value; /* put in the value */
new_node->next = (*head_ref); /* link the old list off the new node */
(*head_ref) = new_node; /* move the head to point to the new node */
}

// Function to get union of two linked lists setA and setB
struct Node* Union(struct Node* A,struct Node* B)
{
struct Node* result = NULL;

// Insert all elements of SetA to the result list
while (A != NULL)

{
push(&result, A-> value);
A = A->next;
}
// Insert those elements of SetB which are not present in result list
while (B != NULL)
{
push(&result, B->value);
B = B->next;
}
return result;
}

/* Function to get intersection of two linked lists setA and setB */
struct Node* Intersection(struct Node* A,struct Node* B)
{
struct Node* result = NULL;

// Traverse SetA and search each element of it in
// SetA. If the element is present in SetB, then insert the element to result
while ( A != NULL)
{
push(&result, A-> value);
A = A->next;
}

return result;
}

/* A utility function to print a linked list*/
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->value);
node = node->next;
}
}

/* program to run above function*/

int main()
{

/* Start with the empty list */
struct Node* A = NULL;
struct Node* B = NULL;
struct Node* intersection = NULL;
struct Node* unin = NULL;

push(&A, 2);
push(&A, 4);
push(&A, 6);
push(&A, 8);

/*create a linked lists with the input data*/
push(&B, 6);
push(&B, 8);
push(&B, 9);
push(&B, 1);

intersection = Intersection(A, B);
unin = Union(A, B);

printf("Set A : ");
printList(A);
printf("\nSet B : ");
printList(B);
printf("\nIntersection : ");
printList(intersection);
printf("\nUnion : ");
printList(unin);

return 0;
}

OUTPUT:

Set A : 8 6 4 2
Set B : 1 9 8 6
Intersection : 2 4 6 8
Union : 6 8 9 1 2 4 6 8

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.