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:
- This code implements a linked list data structure in the C programming language.
- 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.
- 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
- The
insertfunction 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 usingmallocand updates the pointers accordingly to maintain the list's integrity. - The display function traverses the linked list from the head node to the last node and prints the data of each node.
- The union function takes two linked lists as input and returns a new linked list that represents the union of the two input lists.
- The Intersection function takes two linked lists as input and returns a new linked list that represents the intersection of the two input lists.
- 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.
- 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:
-
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.
-
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.
-
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.
-
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:
- 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.
- 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.
- Display: The display function is used to traverse the linked list and print the values of each node.
- 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.
- Search: The search function is used internally by the Union and Intersection functions to check if a given value exists in a linked list.
- 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:
- 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.
- 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.
- 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.
- Display: The code includes the display function, which prints the elements of a linked list in order.
- 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.
- 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:
- 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.
- List Display: After receiving the user input, the program displays the elements of List 1 and List 2 using the display function.
- 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.
- 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.
- 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: