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

Overview of the Project: 

The Overview of the project is as follows:

  1. This code implements a linked list data structure in the C programming language.
  2. It includes functions to insert nodes into the set, display the elements of the set, and perform operations such as finding the union and intersection of two sets.
  3. The code defines a struct called node which represents a node in the linked list. Each node contains an integer value data and a pointer to the next node next
  4. The insert function is responsible for adding a new node with the given data to the end of the linked list. It dynamically allocates memory for the new node using malloc and updates the pointers accordingly to maintain the list's integrity.
  5. The display function traverses the linked list from the head node to the last node and prints the data of each node.
  6. The union function takes two linked lists as input and returns a new linked list that represents the union of the two input lists.
  7. The Intersection function takes two linked lists as input and returns a new linked list that represents the intersection of the two input lists.
  8. The search function is used to search for a given data value in a linked list. It iterates over the nodes of the list and returns 1 if the data is found, and 0 otherwise.
  9. In the main function, the user is prompted to enter the number of elements and the elements themselves for two separate lists. The lists are then displayed. The code then computes the union and intersection of the two lists using the respective functions and displays the resulting lists.

Goals:

The Goals of the project are as follows:

  1. The goal of this code is to demonstrate the operations of creating linked lists, inserting elements into the lists, and finding the union and intersection of two lists.

  2. The code allows the user to input elements for two separate sets. It then creates the linked lists by inserting the entered elements using the insert function. The lists are displayed using the display function.

  3. The code further calculates the union of the two sets using the Union function and displays the result. Similarly, it calculates the intersection of the two sets using the Intersection function and displays the result.

  4. In summary, the purpose of this code is to illustrate the implementation of linked lists and the operations of inserting elements, displaying lists, and finding the union and intersection of two lists.

Key Highlights of the Project:

The Key Highlights of the project are as follows:

  1. Data Structure: The code implements a linked list data structure using the `struct Node` structure. Each node in the list contains an integer value data and a pointer to the next node next.
  2. Insertion: The insert function allows inserting elements at the end of a linked list. It dynamically allocates memory for a new node, assigns the data value, and properly links it to the existing list.
  3. Display: The display function is used to traverse the linked list and print the values of each node.
  4. Union and Intersection: The code provides two functions, Union and Intersection, to compute the union and intersection of two linked lists, respectively. These functions iterate over the nodes of the lists and perform the required operations.
  5. Search: The search function is used internally by the Union and Intersection functions to check if a given value exists in a linked list.
  6. User Interaction: The main function prompts the user to enter the number of elements and the actual elements for two lists. It then calls the necessary functions to perform operations on the lists and displays the results.

Deliverables of this project:

The deliverables of the project are as follows:

  1. Linked List Creation: The code allows the user to create two linked lists by entering the number of elements and the values for each list.
  2. Union List: The code computes the union of the two lists using the Union function. It creates a new linked list that contains all the unique elements from both lists.
  3. Intersection List: The code calculates the intersection of the two lists using the Intersection function. It creates a new linked list that contains the common elements present in both lists.
  4. Display: The code includes the display function, which prints the elements of a linked list in order.
  5. User Interaction: The code prompts the user to enter the number of elements and the actual elements for both lists. It then displays the original lists, as well as the resulting union and intersection lists.
  6. Memory Management: The code properly allocates memory for each node in the linked lists using the `malloc` function.

Expected Outcomes: 

The expected outcomes from the given project are as follows:

  1. User Input: The program prompts the user to enter the number of elements for List 1 and List 2. The user is then asked to input the actual elements for each list.
  2. List Display: After receiving the user input, the program displays the elements of List 1 and List 2 using the display function.
  3. Union List: The program calculates the union of List 1 and List 2 using the Union function. It creates a new linked list that contains all the unique elements from both lists. The resulting union list is displayed using the display function.
  4. Intersection List: The program determines the intersection of List 1 and List 2 using the Intersection function. It creates a new linked list that contains the common elements present in both lists. The resulting intersection list is displayed using the display function.
  5. Output: The program prints the union list and intersection list, providing the combined and common elements respectively.

The expected outcome is an interactive program that allows the user to input two lists, computes the union and intersection of the lists, and displays the results. The output provides insights into the elements present in the original lists and the resulting union and intersection lists.

Please find the code for Project-1 below:


/*
 * p1.c
 *
 *  Created on: 26 Jun 2023
 *      Author: chinmaykadam
 */
 
#include
#include
 
//Defining struct node which contains integer data to store numbers and pointer next to store the address of next node of struct node type
 
struct Node
{
    int data;
    struct Node* next;
};
 
//Defining function 'insert' to add numbers in the linked list from the user
void insert(struct Node** head, int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));    //Creates a new node
    newNode->data = data;    //Stores the data part
    newNode->next = NULL;    //Assigns NULL to the address of newnode
 
    if (*head == NULL)    //Condition to check if the head is NULL then it will store the address of the new node (Happens only 1st time)
    {
        *head = newNode;
    }
    else    //Condition to restore the value of head in temp, and then connecting new node with previous node using temp pointer
    {
        struct Node* temp = *head;    //Assigning head value to temp
        while (temp->next != NULL)    //While loop will run/trace till temp's address is NULL
        {
            temp = temp->next;    //Temp will store the address of that node
        }
        temp->next = newNode;
    }
}
 
//Defining function display to print the linked list/Set
void display(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
 
//Defining function Union() which takes two sets as arguments and returns only the unique numbers present in both sets
struct Node* Union(struct Node* set1, struct Node* set2)
{
    struct Node* result = NULL;    //Declaring result to store the union of sets
    struct Node* temp1 = set1;    //Sets the value of temp1 to the 1st number of set1
    struct Node* temp2 = set2;    //Sets the value of temp2 to the 2nd number of set2
 
    while (temp1 != NULL)
    {
        insert(&result, temp1->data);    //Stores value of set1 elements in result
        temp1 = temp1->next;
    }
 
    while (temp2 != NULL)
    {
        if (!find(result, temp2->data))    //Searches for any repetative number in set1
        {
            insert(&result, temp2->data);    //If not then element is stored in result.
        }
        temp2 = temp2->next;
    }
 
    return result;    //At the end the result is returned with all the unique elements present in both sets
}
 
//Defining function Intersection() which takes two sets(1 and 2) as argument and returns the intersecting elements from both sets
 
struct Node* Intersection(struct Node* set1, struct Node* set2)
{
    struct Node* result = NULL;    //Declaring result to store the intersecting of sets
    struct Node* temp1 = set1;    //Sets the value of temp1 to the 1st number of set1
 
    while (temp1 != NULL) {
        if (find(set2, temp1->data))    //Comparing set2 elements values with set1 elements
        {
            insert(&result, temp1->data);    //If yes, then elements are stored in result
        }
        temp1 = temp1->next;
    }
 
    return result;    //At the end the result is returned with all the common elements present in both sets
}
 
//Defining integer datatype function find() with two arguments to search for elements that are same in both sets
int find(struct Node* head, int data)
{
    struct Node* temp = head;    //Declaring temp as head
    while (temp != NULL)    //This loop will run till the temp reaches the last element of the set
    {
        if (temp->data == data)    //Checking if elements are equal
        {
            return 1;    //If yes it returns 1 else it returns 0;
        }
        temp = temp->next;     //Jumps to the next element
    }
    return 0;
}
 
int main()
{
//Declaring two struct node datatype set1 and set2 to store values in them
    struct Node* set1 = NULL;
    struct Node* set2 = NULL;
 
    int num1, num2, input;    //Initialising integer datatype variables num1, num2, and input.
    printf("Enter the number of elements for List 1: ");    //Taking input from the user for no. of elements and list of elements in the set
    scanf("%d", &num1);
    printf("Enter the elements for Set 1: ");
    for (int i = 0; i < num1; i++)
    {
        scanf("%d", &input);
        insert(&set1, input);    //Calling function insert where the values are stored in set1
    }
 
    printf("Enter the number of elements for Set 2: ");
    scanf("%d", &num2);
    printf("Enter the elements for Set 2: ");
    for (int i = 0; i < num2; i++)
    {
        scanf("%d", &input);
        insert(&set2, input);    //Calling function insert where the values are stored in set1
    }
 
    printf("Set 1: ");
    display(set1);   //Calling function display() to  print the values of set1
 
    printf("Set 2: ");
    display(set2);    //Calling function display() to  print the values of set2
 
    struct Node* unionSet = Union(set1, set2);    //Calling function Union
    printf("Union: ");
    display(unionSet);    //Printing list of union set
 
    struct Node* intersectionSet = Intersection(set1, set2);    //Calling function intersection
    printf("Intersection: ");
    display(intersectionSet);    //Printing list of intersection set
 
    return 0;
}
 
 
Necessary screenshots of output are attached herewith.
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.