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.

Answer:

Steps for generation of code,

  1. Include necessary header files.
  2. Defining data type struct "node" with data variables "data", and a pointer.
  3. constructing functions for creation of node, insertion of nodes, finding same data members in two linked list, intersection and union, and printing.
  4. main function which includes,
  • Initialization of two linked list pointers,local variables,
  • Ask user to enter data in two linked list
  • Create two linked list with data entered by user
  • Calling function to calculate union and intersection
  • Display of results
  • return and exit.

Code:

#include

#include

struct Node {

int data;

struct Node* next;

}; //User defined data type

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

} // creating new node by allocating memory and assigning value

void insertNode(struct Node** head, int data) {

struct Node* newNode = createNode(data);

newNode->next = *head;

*head = newNode;

}//inserting node

//function to search for common data in list 1 and list2

int exists(struct Node* list, int data) {

while (list != NULL) {

if (list->data == data) {

return 1;

}

list = list->next;

}

return 0;

}

//function to find intersection of list 1 and list2

struct Node* intersection(struct Node* list1, struct Node* list2) {

struct Node* result = NULL;

while (list1 != NULL) {

struct Node* temp = list2;

while (temp != NULL) {

if (list1->data == temp->data) {

insertNode(&result, list1->data);

break;

}

temp = temp->next;

}

list1 = list1->next;

}

return result;

}

//function to find intersection of list 1 and list2

struct Node* unionSet(struct Node* list1, struct Node* list2) {

struct Node* result = NULL;

while (list1 != NULL) {

insertNode(&result, list1->data);

list1 = list1->next;

}

while (list2 != NULL) {

if (!exists(result, list2->data)) {

insertNode(&result, list2->data);

}

list2 = list2->next;

}

return result;

}

//function to print linked list

void printList(struct Node* list) {

while (list != NULL) {

printf("%d ", list->data);

list = list->next;

}

printf("\n");

}

int main() {

struct Node* list1 = NULL;//declaring empty node for Linked list 1

struct Node* list2 = NULL;//declaring empty node for Linked list 2

int n, m, data;

printf("Enter the number of elements in the first list:\n ");

fflush(stdout);

scanf("%d", &n);

printf("Enter the elements of the first list:\n");

fflush(stdout);

for (int i = 0; i < n; i++) {

scanf("%d", &data);

insertNode(&list1, data);

}

printf("Enter the number of elements in the second list:\n ");

fflush(stdout);

scanf("%d", &m);

printf("Enter the elements of the second list:\n");

fflush(stdout);

for (int i = 0; i < m; i++) {

scanf("%d", &data);

insertNode(&list2, data);

}

printf("Intersection of the two lists: ");

struct Node* inter = intersection(list1, list2);

printList(inter);

printf("Union of the two lists: ");

struct Node* uni = unionSet(list1, list2);

printList(uni);

return 0;

}

Result:

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.