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

Source Code-

#include 

// To access malloc function for memory allocation
#include 

// To access bool data type 
#include 

struct Node {
    int data;
    struct Node* next;
};
  

void push(struct Node** headref, int Data);
bool isPresent(struct Node* head, int data);

  
// Union of two linked lists head 1 & head 2 .
struct Node* retUnion(struct Node* head1,struct Node* head2){

    struct Node* res = NULL;
    struct Node *temp1 = head1 ;
    struct Node *temp2 = head2;
  
    // Insert all elements of list1 to result list
    while (temp1 != NULL) {
        push(&res, temp1->data);
        temp1 = temp1->next;
    }
  
    // Insert those elements of list2 which are not present in result list
    while (temp2 != NULL) {
        if (!isPresent(res, temp2->data))
            push(&res, temp2->data);
        temp2 = temp2->next;
    }
  
    return res;
}
  
// Intersection of two linked lists head1 and head2 .
struct Node* retIntersectn(struct Node* head1, struct Node* head2){

    struct Node* res = NULL;
    struct Node* temp1 = head1;
  
    // Traverse list1 and searching its element in list2 & then return .
    while (temp1 != NULL) {
        if (isPresent(head2, temp1->data))
            push(&res, temp1->data);
        temp1 = temp1->next;
    }
  
    return res;
}

// Function to insert a node at the beginning ofa linked list .  
void push(struct Node** headref, int Data){
    
    struct Node* Newnode = (struct Node*)malloc(sizeof(struct Node));
  
    Newnode->data = Data;
    Newnode->next = (*headref);
    (*headref) = Newnode;
}
  
 
// Function that returns true if data is present in linked list else return false
bool isPresent(struct Node* head, int data){

    while (head != NULL) {
        if (head->data == data){
            return 1;
        }    
        head = head->next;
    }
    return 0;
}

// Function to print a linked list .
void printList(struct Node* node){
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
  
int main(){

    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
    struct Node* inter = NULL;
    struct Node* uni = NULL;
  
    push(&head1, 12);
    push(&head1, 8);
    push(&head1, 45);
    push(&head1, 20);
  
    
    push(&head2, 12);
    push(&head2, 3);
    push(&head2, 6);
    push(&head2, 8);
  
    inter = retIntersectn(head1, head2);
    uni = retUnion(head1, head2);
  
    printf("\n Elements in First list are : ");
    printList(head1);
  
    printf("\n Elements in Second list are : ");
    printList(head2);
  
    printf("\n Intersection of the two list is : ");
    printList(inter);
  
    printf("\n Union of the two list is : ");
    printList(uni);
  
    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.