Challenge

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

Solution:

Overview of the project:

Here the main objective of the project is to do the mathematical set operation. The set operation has many operations here they are told to do the union and intersection of the list crerated  as a linked lisr. so here we are creating a structure it has a collecion different dara. Here is the process to first creare two lisrs using the reference address. The main concept of linkedlist is self referential structure.

Implementation Of C interface for mathematical set theory using C progamming:

First, we are creating the structure as a tyoedef model to rename the user defined name.

In that, we are creating two datatypes int for storing the integer value, and the pointer variable is used it storing the address of the same structure itself.

And after we are creating the main function we get the values of list 1 anf list 2 using the for loop and using the many function call in that.

And then we are creating the different functions wha are mentioned in the main program.

Creating two lists of data using the linked list method:

Firsr, we are getting the size of the list. Using the for loop we are inserting the data inside the foe loop, we use the insert node function.

In these functions, we are using arguments are heas address and value from the user.

Inside the inserted node, we are creating the size of the lisr using the malloc funciton.

Entering the data in the data area, and storing the address of the head to the next pointer and we are storing the new node address in the head for the next update.

Likewise, we are creating the two lists.

Getting Union of two lists:

Here, we get a function call of getting union with the argument of head1 and head 2

In the union function , we are using the head unit as a list by inserting the value of the first list in the union and jumping to the next area by addressing stores in the next.

Insert node funcgtion as same as the function which is used for the creation of the inked lisr.

Using another while loo[ we are getting the second list. Using the compare function using not we are getting only the value that is not placed in the list one and inserted in the unionn in the list.

Getting Intersection of two lists.

In the intersection, we are easy to get an intersection because just we compare to list and return the value which is placed in both lists commonly.

Using the if command comparing the data of lists 1 and 2

If the condition is true it passes the next step which has an intersection list and using the insert function we are inserting the data into that list.

Display function:

It is used to display the data of teh different lists by using different arguments passing to the display function.

So creating the temaparary list with the head is null. And print data and use the next address stored in the pointer variable pointing to the structure.

if head next has it recognize it has last node.

Full Code:

#include

#include

#include

typedef struct node//defining the structure

{

                  int data;// creating the data type of the variable

                  struct node *next;// creating self referential node

}

node;// changing the structure name as node

static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersecton;// declaring the different varable in structure type

/*Declaring different function with arguments*/

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,Nide *head_pointer2);

int compare(Node *head, int data);

void display_list(Node *head_ref)// function used to display the nodes data

{

                       if(head_ref == 0)

{

                       printf(" List is empty. ");

}

else

{

                   tail = head_ref;// we are transferring address if the list to tail

                   while(tail != 0)

                   {

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

                          tail = tail->next;// advances the position of current node

                   }

                   tail = newnode;

}

Node *insert_node(Node *head_ref, int n)// functionused create the new list

{

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

                         newnode =(Node*) malloc(size of (Node));//allocating the memory to the nodes

                         printf("Address of created newnide :%dn",newnode);

                         newnode->data = n;//assigning the value to node

                         printf("Inserted value is :%dn", newnode->data);

                         newnode->next = head_ref;//address of the previous address s transfered to the newnode

                         head_ref = newnode;//transfering the new address to the head ref for linking new node 

                         return head_ref;

}

Node *get_union(Node *head_pointer1,Node *head_pointer2)//functionused to 

union to list

{

                          while(head_pointer1 !=0)

                          {

                                                   head_union = insert_node(head_union,head_pointter1->data);//here we are inserting the data of list 1 into the union function

                                                   head_pointer1 = head_pointer1->next;//we transverse the node using the address in next.

                          }

                          while(head_ponter2 != 0)

                          {

                                                If( !compare(head_union,head_pointer2->data))//Here we are inserting the element from list 2 number where in the list1 to the union

                                                                             head_union = insert_node(head_union,head_pointer2->data);//inserting the elementnot in the list2

                                                 head_pointer2 = head_pointer2->next;//travelling to next node

                          }

                          return head_union;

  }

int compare(Node *head,int data)

{

         while (head != NULL)

         {

                  if (head->data ==data)//comparing the data of list 2 with list 1

                           return 1;

                 head = head->next;

          }

        return 0;

}

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

{

        while (head_pointer1 != 0)

           {

                  if  (compare(head_pointer2, head_pointer1->data))//we are comparing the both list for intersection

                                 head_intersection = insert_node(head_intersection,head_pointer1->data);//transferring the value to the intersection

                          head_pointer1 = head+pointer1->next;

           }

           return head_intersection;

}

int main()

{

                                           int first_list_nodes = 0, second_list_nodes = 0, first_list_items = 0, second_list_items = 0;

                                           int f;

                                           printf("Enter the number of Nodes in first List:");

                                           fflush(stdout);

                                           scanf("%d",&first_list_nodes);

                                           printf("Enter %d the elements:", first_list_nodes);//getting the number of element in the lists

                                           for(f=0; f

                                           {

                                                             fflush(stdout);

                                                             scanf("%d",&first_list_items);//getting the data in run time

                                                             head1 = insert_node(head1,first_list_items);//inserting the data in list

                                            }

                                            display_list(head1);//displaying the head1 list

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

                                            fflush(stdout);

                                            scanf("%d",&second_list_nodes);

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

                                            for(f=0; f

                                           {

                                                      fflush(stdout);

                                                      scanf("%d",&second_list_items);//getting the data in the run time

                                                       head2 = insert_node(head2, second_list_items);

                                           }

                                           display_list(head2);//displaying the head1 list

                                           res_union = get_union(head1 , head2);//transferring the result of union function to the res union

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

                                           display_list(res_union);//transferring the result of intersection to the res intersection

                                           res_intersection = get_intersection(head1, head2);//transferring the result of intersection function to res intersecton

                                           printf("Here is the result of intersection:");//transferring the result of intersection function to the res intersection

                                           display_list(res_intersection);//transferring the values as arguments to display 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.