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

  • 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.

ANSWER

/*
 * project_1_Creating_two_list _of_data_using_linked_list_method.c
 * Created on: 26-Aug-2023
 * Author: SAVITA BHAGAT
 */
 
#include      //We have to include for standard input/output functions.
#include     //We have to include for memory allocation and other utility
 
struct Node { // we have defined structer struct Node to represent a node in the linked list which contains two members to store the value of the node.(integer data)
    int data;
    struct Node* next;      // we have defined * next (pointer) to point the next node in the a node in the linked list.
};
 
void insertEnd(struct Node** head, int data) {   // The insertEnd function is used to insert a new node with the given data at the end of the linked list.
                                          // A double pointer to head (a pointer to the pointer that points to the first node of the list) and the data to be inserted.
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
 
    if (*head == NULL) {
        *head = newNode;
        return;
    }
 
    struct Node* current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
}
 
void printList(struct Node* head) {  //The printList function is used to print each the elements data  of a linked list.It takes a pointer to the head of the list and                                       traverses through the list
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}
 
int search(struct Node* head, int key) { //The search function searches for a given key in the linked list. It takes a pointer to the head of the list .
    struct Node* current = head;
    while (current != NULL) {
        if (current->data == key) {
            return 1;                   // If returns 1 then the key is found.
    }
        current = current->next;
    }
    return 0;                           // If returns 0then the key is not found.
}
 
struct Node* findUnion(struct Node* list1, struct Node* list2) {  //The findUnion function computes the union of two linked lists.pointers to list1 and list2 as                                                                           arguments.
    struct Node* result = NULL;
    struct Node* current1 = list1;
    struct Node* current2 = list2;
 
    while (current1 != NULL) {
        insertEnd(&result, current1->data);
        current1 = current1->next;
    }
 
    while (current2 != NULL) {
        if (!search(result, current2->data)) {
            insertEnd(&result, current2->data);
    }
        current2 = current2->next;
    }
 
    return result;
}
 
struct Node* findIntersection(struct Node* list1, struct Node* list2) {    // The findIntersection function computes the intersection of two linked lists. It takes                                                                            pointers to list1 and list2 as arguments.
 
    struct Node* result = NULL;
    struct Node* current1 = list1;
 
    while (current1 != NULL) {
        if (search(list2, current1->data)) {         // if an element is found in list2, it adds it to a new list representing the intersection.
            insertEnd(&result, current1->data);
    }
        current1 = current1->next;
    }
 
    return result;
}
 
int main() {                        // We have declared and initializes two pointers list1 and list2 which will represent the heads of the two linked lists.
    struct Node* list1 = NULL;
    struct Node* list2 = NULL;
 
    insertEnd(&list1, 1);           // Elements (1, 2, 3,4,5) are inserted into list1 using the insertEnd function.
    insertEnd(&list1, 2);
    insertEnd(&list1, 3);
    insertEnd(&list1, 4);
    insertEnd(&list1, 5);
 
    insertEnd(&list2, 2);           // Elements ( 2, 3,4,5,6,7) are inserted into list2 using the insertEnd function.
    insertEnd(&list2, 3);
    insertEnd(&list2, 4);
    insertEnd(&list2, 5);
    insertEnd(&list2, 6);
    insertEnd(&list2, 7);
 
    printf("List 1: ");
    printList(list1);              // The original contents of first list is  printed using the printList function.
 
    printf("List 2: ");
    printList(list2);              // The original contents of second  list is  printed using the printList function.
 
    struct Node* unionResult = findUnion(list1, list2);  //The findUnion function is called to find the union of the two lists, the result is stored in                                                                           unionResult.
    printf("Union: ");
    printList(unionResult);
 
    struct Node* intersectionResult = findIntersection(list1, list2); //The findIntersection function is called to find the intersection of the two lists, and the                                                                         result is stored in intersectionResult.
    printf("Intersection: ");
    printList(intersectionResult);                                    //The results of the union and intersection are printed using the printList function.
 
 
    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.