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: (5 to 6 lines)

In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists

Goals (2 to 3 points)

  • Implementation of interface for mathematical Set Theory using C programming.
  • Giving two list of data as input to get the output as union & intersection for given list of data.

Key Highlights of the Project: (4 to 5 point)

  • Creating two list of data using linked list method.
  • Insert the node to 1st and 2nd list accordingly.
  • Intersection of data of two lists.
  • Getting Union of two list.

Deliverables of this project: (4 to 5 points)

  • Intersection of data of two list.
  • Union of data of two list

Expected Outcomes: (4 to 5 points)

  • Student will get the Knowledge of creating the nodes using Linked List.
  • Getting the intersection and Union of data of two list.
  • Comparing the elements of two list.

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

// Link list node
struct Node
{
int data;
struct Node* next;
};
//function declaration

void push(struct Node** head_ref, int new_data);

isPresent(struct Node* head, int data);

// Function to get union of two linked lists LL1 and LL2

struct Node* getUnion(struct Node* LL1,struct Node* LL2)
{
struct Node* result = NULL;
struct Node *t1 = LL1, *t2 = LL2;

// Insert all elements of list1 to the result list

while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list

while (t2 != NULL)
{
if (!isPresent(result, t2->data))
push(&result, t2->data);
t2 = t2->next;
}

return result;
}
// Function to get intersection of two linked lists LL1 and LL2


struct Node* getIntersection(struct Node* LL1,struct Node* LL2)
{
struct Node* result = NULL;
struct Node* t1 = LL1;
// Traverse list1 and search each element of it in list2. If the element is present in list 2,
//then insert the element to result

while (t1 != NULL)
{
if (isPresent(LL2, t1->data))
push(&result, t1->data);
t1 = t1->next;
}

return result;
}
// A utility function to insert a node at the beginning of a linked list


void push(struct Node** head_ref, int new_data)
{
// allocate node

struct Node* new_node= (struct Node*)malloc(sizeof(struct Node));

// put in the data

new_node->data = new_data;

// link the old list off the new node

new_node->next = (*head_ref);
// move the head to point to the new node


(*head_ref) = new_node;
}
// function to print a linked list

void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}

// function that returns true if data is present in linked list else return false

isPresent(struct Node* LL, int data)
{
struct Node* t = LL;
while (t != NULL) {
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}

// Driver program to test above function

int main()
{
int a,b,c,d,w,x,y,z;

// structure pointer with the empty list


struct Node* LL1 = NULL;
struct Node* LL2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;

printf("Enter the 4 elements LL1 ");

fflush(stdout);

scanf("\n%d",&a);
scanf("\n%d",&b);
scanf("\n%d",&c);
scanf("\n%d",&d);

printf("Enter the 4 elements LL2");

fflush(stdout);

scanf("\n%d",&w);
scanf("\n%d",&x);
scanf("\n%d",&y);
scanf("\n%d",&z);

push(&LL1, a);
push(&LL1, b);
push(&LL1, c);
push(&LL1, d);


push(&LL2, w);
push(&LL2, x);
push(&LL2, y);
push(&LL2, z);

intersecn = getIntersection(LL1, LL2);
unin = getUnion(LL1, LL2);

printf("\n First list is \n");
printList(LL1);

printf("\n Second list is \n");
printList(LL2);

printf("\n Intersection list is \n");
printList(intersecn);

printf("\n Union list is \n");
printList(unin);

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.