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.
/*
 * Set_theory.c
 *
 *  Created on: Apr 27, 2024
 *      Author: anups
 */
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

// Structure representing a set
typedef struct {
    int elements[MAX_SIZE];
    int size;
} Set;

// Function to create an empty set
Set createSet() {
    Set newSet;
    newSet.size = 0;
    return newSet;
}

// Function to check if an element is present in the set
int isMember(Set set, int element) {
    for (int i = 0; i < set.size; i++) {
        if (set.elements[i] == element) {
            return 1; // Element found
        }
    }
    return 0; // Element not found
}

// Function to add an element to the set
void addElement(Set *set, int element) {
    if (!isMember(*set, element)) {
        set->elements[set->size++] = element;
    }
}

// Function to find the union of two sets
Set setUnion(Set set1, Set set2) {
    Set unionSet = createSet();
    for (int i = 0; i < set1.size; i++) {
        addElement(&unionSet, set1.elements[i]);
    }
    for (int i = 0; i < set2.size; i++) {
        addElement(&unionSet, set2.elements[i]);
    }
    return unionSet;
}

// Function to find the intersection of two sets
Set setIntersection(Set set1, Set set2) {
    Set intersectionSet = createSet();
    for (int i = 0; i < set1.size; i++) {
        if (isMember(set2, set1.elements[i])) {
            addElement(&intersectionSet, set1.elements[i]);
        }
    }
    return intersectionSet;
}

// Function to print a set
void printSet(Set set) {
    printf("{ ");
    for (int i = 0; i < set.size; i++) {
        printf("%d ", set.elements[i]);
    }
    printf("}\n");
}

int main() {
    Set set1 = createSet();
    Set set2 = createSet();

    // Add elements to set1
    addElement(&set1, 1);
    addElement(&set1, 2);
    addElement(&set1, 3);

    // Add elements to set2
    addElement(&set2, 3);
    addElement(&set2, 4);
    addElement(&set2, 5);

    // Print sets
    printf("Set 1: ");
    printSet(set1);
    printf("Set 2: ");
    printSet(set2);

    // Find and print union
    printf("Union: ");
    printSet(setUnion(set1, set2));

    // Find and print intersection
    printf("Intersection: ");
    printSet(setIntersection(set1, set2));

    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.