Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

#include <stdio.h> // standard input output header file
#include <stdlib.h> // standard library functions

struct node // structure templet
{
    int data; //structure member
    struct node *link;
};
// fun prototype
struct node* insert_node(struct node *head_ref,int data);
void disply_list(struct node *head_ref);
void union_node(struct node *head1_ref,struct node *head2_ref);
void intsec_node(struct node *head1_ref,struct node *head2_ref);
int compare(struct node *head_ref,int data);

static struct node *head1,*head2,*union_list,*intsec,*end, *newnode, *nextnode; // decl of fun pointer

int main() // main fun
{
   int node_num=0,data; // variable decl

   printf("\nenter the no of node in 1st linked list : "); // prints the nodes in 1st list
   scanf("%d",&node_num); // reads no of nodes in 1st list

for(int i=1;i<=node_num;i++) // for loop insert the data inside the 1st node
{
    printf("enter the node data : ");
    scanf("%d",&data); // ask data from user
    head1 = insert_node(head1,data); // store the data in list 1
}

printf("\nenter the no of node in 2nd linked list : "); // prints the nodes in 2nd list
scanf("%d",&node_num); // reads no of nodes in 2nd list

 for(int i=1;i<=node_num;i++) // for loop insert the data inside the 2nd node
{
    printf("enter the node data : ");
    scanf("%d",&data);// ask data from user
    head2 = insert_node(head2,data); // stores the data in list 2
}
printf("linked list 1 : \n"); // enters the no.of nodes in 1st list

display_list(head1); // sub fun call

printf("linked list 2 : \n"); // enters the no.of in 2nd list

display_list(head2); // sub fun call

printf("union in the list : \n"); // enters union list

union_node(head1,head2); // sub fun call for check the union of list

display_list(union_list); // sub fun call for display union list

printf("intersection of the list 1 and 2 : \n"); // enters intersection list

intsec_node(head1,head2); // sub fun call to insersection the data

display_list(intsec); // sub fun call
}
// fun for insert the new nodes
struct node* insert_node(struct node *head_ref,int data)
  {
    nextnode = head_ref; // head add store to next node
    newnode =(struct node*)malloc(sizeof(struct node)); // create the new node
    newnode->data = data; // data into newnode
    newnode->link = nextnode; // nextnode add store to newnode link
    nextnode = newnode; // newnode store to nextnode

    return nextnode; // executes & returns from node
  }

void display_list(struct node *head_ref) // display fun for displaying the data inside the node
{


    if(head_ref == 0) // if condition
        printf("List is empty");
    else
    {
        end =  head_ref;
        while(end != 0) // while loop
        {
            printf(" %d\n",end->data);
            end = end->link;
        }
    }

}
void union_node(struct node *head1_ref,struct node *head2_ref) // check the unions
{
    union_list = NULL;

    while(head1_ref != NULL)
    {
        union_list = insert_node(union_list,head1_ref->data);
        head1_ref = head1_ref->link;
    }

    while(head2_ref != NULL)
    {
        if(!compare(union_list,head2_ref->data))
            union_list = insert_node(union_list,head2_ref->data); // it compare if it is not equal it add to the list

        head2_ref = head2_ref->link;
    }
}
void intsec_node(struct node *head1_ref,struct node *head2_ref) // check the intersection of list
{
    intsec = NULL;

    while(head2_ref != NULL)
    {
        if(compare(head1_ref,head2_ref->data))
            intsec = insert_node(intsec,head2_ref->data); // it will check if it is same it will add to the list

        head2_ref = head2_ref->link;
    }
}
int compare(struct node *head_ref,int key)
{
    struct node *cmpr = head_ref;

    while( cmpr != NULL)
    {
        if(cmpr->data == key) // checks condition
            return 1;
        cmpr = cmpr->link;
    }
    return 0; // executes & exits out of the function

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