Challenge

Interfaces for working with ‘Set’ using Linked List

#include
#include
//defining a node
//data, adress of next node

typedef struct node
{
    int data;
    struct node *next;  //self refrential structure
   
   
}Node;


static Node *head1, *head2, *head, *head_union, *head_intersection,*head_ref, *newnode,*tail, *res_union, *res_intersection;
//defining function
//insert node, get union, get intersection, compare, display


Node *insert_node(Node *head_ref,int n);
Node *get_union(Node *head_pointer1, Node *head_pointer2);
Node *get_intersection(Node *head_pointer1, Node *head_pointer2);
void display_list(Node *head_ref);
int compare(Node *head, int data);

int main()
{
    int first_list_nodes = 0, second_list_nodes = 0, first_list_items = 0,
        second_list_items = 0;
   
   
    printf("\nEnter the number of Nodes in first List:");
    fflush(stdout);
    scanf("%d",&first_list_nodes);
    printf("\nEnter %d elements:",first_list_nodes);
    for(int i = 0; i< first_list_nodes;i++)
    {
        fflush(stdout);
        scanf("%d",&first_list_items);
        head1 = insert_node(head1,first_list_items);
    }
    display_list(head1);

   
    printf("\nEnter the number of Nodes in second List:");
    fflush(stdout);
    scanf("%d", &second_list_nodes);
    printf("\nEnter %d elements:", second_list_nodes);
    for (int i = 0; i < second_list_nodes; i++)
    {
        fflush(stdout);
        scanf("%d", &second_list_items);
        head2 = insert_node(head2, second_list_items);
    }
    display_list(head2);


    res_union = get_union(head1,head2);
    printf("\nHere is the result of union:");
    display_list(res_union);


    res_intersection = get_intersection(head1,head2);
    printf("\nHere is the result of intersection:");
    display_list(res_intersection);

}

Node *insert_node(Node *head_ref,int n)
{
    //printf("\ninserted nodes at the beginning");
    newnode=(Node*)malloc(sizeof(Node));
    //printf("\naddress of the node is %p",newnode);
    newnode->data=n;
    //printf("\ninserted value is %d\n",newnode->data);
    newnode->next=head_ref;
    head_ref=newnode;
    return head_ref;
}

Node *get_union(Node *head_pointer1, Node *head_pointer2)
{
    while (head_pointer1 != 0)
    {
        head_union = insert_node(head_union,head_pointer1->data);
        head_pointer1 = head_pointer1->next;

    }
    while (head_pointer2 != 0)
    {
        if(!compare(head_union,head_pointer2->data))
            head_union = insert_node(head_union,head_pointer2->data);
        head_pointer2 = head_pointer2->next;
   
    }
    return head_union;
   
}
int compare(Node *head, int data)
{
    while (head != NULL)
    {
        if (head->data == data)
            return 1;
        head = head->next;
    }
}
Node *get_intersection(Node *head_pointer1, Node *head_pointer2)
{
    while(head_pointer1!= 0)
    {
        if (compare(head_pointer2, head_pointer1->data))
            head_intersection = insert_node(head_intersection, head_pointer1->data);
        head_pointer1 = head_pointer1->next;
    }
    return head_intersection;
}


void display_list(Node *head_ref)
{
    if(head_ref == 0)
    {
        printf("\nList is empty");
    }
    else
    {
        //tail = head_ref;
        while(head_ref != 0)
        {
            printf("%d",head_ref->data); //prints the data of current node
            head_ref = head_ref->next;
        }
        head_ref=newnode;

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