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
#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,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;//declaring the different variable in structure type
/* Declaring diffferent function with the argumrnts*/

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

void display_list(Node *head_ref)      //function used to dispaly the nodes data .
{
 if(head_ref == 0)
{
     printf(" List is empty.");
     fflush(stdout);
}
else
{
       tail = head_ref;                 //we are transfering address if the list to tail
while(tail != 0)
{
       printf("%d ", tail->data);       // prints the data of current node
       fflush(stdout);
       tail = tail->next;                // advances the position of current node
}
       tail = newnode;
}
}

Node *insert_node(Node *head_ref,int n)    //function used create the new list
{
        printf("\nInserting Elements at the beginning...\n");
        fflush(stdout);
        newnode =(Node*) malloc(sizeof(Node));        //allocating the memory to the nodes
        printf("Address of created newnode :%d\n",newnode);
        fflush(stdout);
        newnode->data = n                            ;//assigning the value to node
        printf("Inserted value is :%d\n",newnode->data);
        fflush(stdout);
        newnode->next = head_ref;          //address of the pervious address is transfered to the new node
        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)    //function used to union to list
{
while(head_pointer1 != 0)
{
head_union = insert_node(head_union,head_pointer1->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_pointer2 != 0)
{
if(!compare(head_union,head_pointer2->data))         //Here we are insering the element from list 2 number__
	                                                       //__where not in the in th list 1 to the union

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

     head_pointer2 = head_pointer2->next;        //jump 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 instersection
 head_intersection = insert_node(head_intersection,head_pointer1->data);  //transferring the that 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
       fflush(stdout);
 for( f = 0;f < first_list_nodes;f++)
{

	 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);                                 //displating 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);
     fflush(stdout);
 for(f = 0;f < second_list_nodes;f++)
 {

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

     display_list(head2);                       //displating the head1 list
     res_union = get_union(head1,head2);        //transfering the result of union function to the res union
     printf("Here is the result of union: ");
     fflush(stdout);
     display_list(res_union);                    //transfring the values as argumnets to display function
     res_intersection = get_intersection(head1,head2);   //transfering the result of intersection function to the res intersection

     printf("Here is the result of intersection: ");//transfering the result of intersection function to the res intersection
     fflush(stdout);
     display_list(res_intersection);               //transfring the values as argumnets to display function

}

Output


Enter the number of Nodes in first List:5
Enter 5 the elements:1

Inserting Elements at the beginning...
Address of created newnode :16588360
Inserted value is :1
2

Inserting Elements at the beginning...
Address of created newnode :16588376
Inserted value is :2
3

Inserting Elements at the beginning...
Address of created newnode :16588392
Inserted value is :3
4

Inserting Elements at the beginning...
Address of created newnode :16588408
Inserted value is :4
5

Inserting Elements at the beginning...
Address of created newnode :16588424
Inserted value is :5
5 4 3 2 1 
Enter the number of Nodes in second List:3
Enter 3 the elements:1

Inserting Elements at the beginning...
Address of created newnode :16588440
Inserted value is :1
4

Inserting Elements at the beginning...
Address of created newnode :16586496
Inserted value is :4
7

Inserting Elements at the beginning...
Address of created newnode :16586512
Inserted value is :7
7 4 1 
Inserting Elements at the beginning...
Address of created newnode :16586528
Inserted value is :5

Inserting Elements at the beginning...
Address of created newnode :16586544
Inserted value is :4

Inserting Elements at the beginning...
Address of created newnode :16586560
Inserted value is :3

Inserting Elements at the beginning...
Address of created newnode :16586576
Inserted value is :2

Inserting Elements at the beginning...
Address of created newnode :16586592
Inserted value is :1

Inserting Elements at the beginning...
Address of created newnode :16586608
Inserted value is :7
Here is the result of union: 7 1 2 3 4 5 
Inserting Elements at the beginning...
Address of created newnode :16586624
Inserted value is :4

Inserting Elements at the beginning...
Address of created newnode :16586640
Inserted value is :1
Here is the result of intersection: 1 4 

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.