Challenge

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

Ans: Here, we need to insert the nodes, and get the union, and intersection of those nodes.

here i created the two lists, where i can insert the no.of nodes i want.

first i define the node, then i defined some subfunctions, and global variables.

in the main function, first i initialise the list of items and nodes with '0'

then i ask for no.of nodes i want to inserted and the values of those nodes.

then by using for loop, i inserted nodes in head1, until the statemt satisifies., first it will scan of list items then it is inserted in head1. By using display list i displayed those list items in head1.

similarly for second list. next by calling union and intersection subfunctions i displayed the union and intersection of elements in two lists.

the logic for dispaly list is:here i take a head_ref pointer, head_ref contains the node data.if it is = 0, it says list is empty, else it will do some action, i.e, head_ref value will go to tail(variable), now tail!=0, it prints the current node data in tail, and then next node will come to tail, i.e, tail=newnode.

the logic for insert node is, here it allocates some memory to the new node, and we print the address of new node here, to make sure whether the node is inserted in new address or same as previous, so that we can know that it wont disturb the preveious nodes.once it prints the new node data, next data will come inside it and allocation occurs and prints it.

in the union subfunction, the data in head1 and head2 will both come out as union of those two lists, if we had same nodes present in two heads, it will consider that as one node, and prints the output.i.e here it will check the nodes present in two heads, if the same value present it will take only one i.e head1 = 5, 6, 7 and head2= 3 4 5, it will check every elemtn i.e, 3 present in head 2 or not if not it'll print like that it will check for every elemnt, since 5 present in two heads, it'll take only one time,so the output is 3,4,5,6,7.

in the intersectioon subfunction, it compares the data in two heads and gives th eoutput. i.e.,intersection is  nothing but it will throw the common nodes between head1 and head2 here it compares the data between head1 and head2 , if any node is equal it will give the result that contains that node.

i.e., head1=5,6,7;head2=3,4,5, it will give output as 5.

Code:

#include

#include

#include

/*-----Define the Node-----*/

typedef struct node

{

int data;

struct node *next;

}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;

/*-----Define the functions-----*/

Node *insert_node(Node *head_ref,int n);

void display_list(Node *head_ref);

Node *get_union(Node *head_pointer1,Node *head_pointer2);

Node *get_intersection(Node *head_pointer1,Node *head_pointer2);

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("Enter the number of Nodes in first List:");

fflush(stdout);

scanf("%d",&first_list_nodes);//scans the entered value

printf("Enter %d the elements:",first_list_nodes);

for(int i = 0;i < first>

{

fflush(stdout);

scanf("%d",&first_list_items);//scan for list items

head1 = insert_node(head1,first_list_items);// inserting nodes in head1

}

display_list(head1);// print the list of elements present in head1

printf("\nEnter the number of Nodes in second List:");

fflush(stdout);

scanf("%d",&second_list_nodes);//scans for entered value

printf("Enter %d the elements:",second_list_nodes);

for(int i = 0;i < second>

{

fflush(stdout);

scanf("%d",&second_list_items);

head2 = insert_node(head2,second_list_items);// inserting gives node values in head2

}

display_list(head2);// displays the list of elements presentin head2

res_union = get_union(head1,head2);// union of head1 and head2 elements will store in res_union

printf("Here is the result of union: ");

display_list(res_union);// displays the union of elements in two lists

res_intersection = get_intersection(head1,head2);// intersection i.e common elements in head1 and head2 will store

//in res_intersection

printf("Here is the result of intersection: ");

display_list(res_intersection);// displays the intersection of elements that stores in res_intersection

}

void display_list(Node *head_ref)// sub-function expansion

{

   if(head_ref == 0)//if head_ref=0, it throws the output as list is empty, else it will do some action

   {

       printf(" List is empty.");

   }

   else

   {    // the head_ref contains the node data, 

       tail = head_ref;// the values of head_ref store in tail

       while(tail != 0)//if tail !=0 it will print current node data in tail

       {

           printf("%d ", tail->data);// prints the data of current node

           tail = tail->next;// advances the position of current node, i.e next node data will go to tail

       }

       tail = newnode;

   }

}

Node *insert_node(Node *head_ref,int n)//sub-function

{

printf("\nInserting Elements at the beginning...\n");

newnode =(Node*) malloc(sizeof(Node));//allocating memory to the newnode

printf("Address of created newnode :%d\n",newnode);

newnode->data = n;

printf("Inserted value is :%d\n",newnode->data);//prints the new node data

newnode->next = head_ref;// next node data will went  to head_ref

head_ref = newnode;//i.e head_ref is equal to newnode

return head_ref;

}

Node *get_union(Node *head_pointer1,Node *head_pointer2)// union subfunction

{

/*here it will check the nodes present in two heads, if the same value present it will take only one 

i.e head1 = 5, 6, 7 and head2= 3 4 5, it will check every elemtn i.e, 3 present in head 2 or not if not it'll print 

like that it will check for every elemnt, since 5 present in two heads, it'll take only one time,so the 

output is 3,4,5,6,7*/

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;

}

//to compare data inside the head1 and head2, we use this

int compare(Node *head,int data)

{

    while (head != NULL)

    {

        if (head->data == data)

            return 1;

        head = head->next;

    }

    return 0;

}

/* intersection is  nothing but it will throw the common nodes between head1 and head2

here it compares the data between head1 and head2 , if any node is equal it will give the result that contains that node.*/

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;

}

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.