Challenge

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

1)Link list

#ifndef HEADER_H_//used defined header file

#define HEADER_H_

#include <stdio.h> //header file

#include <stdlib.h> // header file

typedef struct node// initializing typedef Structure node

{

int data;// initializing data type

struct node *next; // *pointing the address

}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;//declaring the pointer variables

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

void display_list(Node *head_ref); // Displaying

Node *get_union(Node *head_pointer1,Node *head_pointer2); // declaring the union

Node *get_intersection(Node *head_pointer1,Node *head_pointer2);//declaring the intersection

int compare(Node *head,int data); //declaring the compare

#endif /* HEADER_H_ */

#include "header.h"//calling the user defined header file

int main()// Main Funtion

{

int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,second_list_items = 0;//initializing the variables

printf("Enter the number of Nodes in first List:"); //printing on console

fflush(stdout);

scanf("%d",&first_list_nodes); // scanning from user

printf("Enter the %d elements:",first_list_nodes); // Displaying on screen, Getting elements from User

for(int i = 0;i < first_list_nodes;i++)

{

fflush(stdout);

scanf("%d",&first_list_items); // Getting first_list_items from user

head1 = insert_node(head1,first_list_items); // head1 is Variable name for first list

}

display_list(head1); // Displaying in head1

printf("\nEnter the number of Nodes in second List:");// Getting the number of nodes from User for second list

fflush(stdout);

scanf("%d",&second_list_nodes);

printf("Enter the %d elements:",second_list_nodes); // Displaying on screen, Getting the elements from User

for(int i = 0;i < second_list_nodes;i++)

{

fflush(stdout);

scanf("%d",&second_list_items); // Getting second_list_items

head2 = insert_node(head2,second_list_items); // head2 is Variable name for second list

}

display_list(head2); // Displaying in head2

res_union = get_union(head1,head2); // res_union as variable name

printf("\nThe result of union: ");// Displaying the results of union on screen

display_list(res_union);// Displaying res_union

res_intersection = get_intersection(head1,head2); // res_intersection as variable name

printf("\nThe result of intersection: ");// Displaying the results of intersection on screen

display_list(res_intersection); // Displaying res_intersection

}

void display_list(Node *head_ref)

{

if(head_ref == 0) // head_ref is equal to 0. It goes inside the bracket and prints the statement

{

printf(" List is empty.");

}

else

{

tail = head_ref;

while(tail != 0)// tail should not equal to 0 then only it goes inside bracket otherwise it goes outside

{

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

tail = tail->next; // Process will be repeated

}

tail = newnode;

}

}

#include "header.h"

Node *insert_node(Node *head_ref,int n)

{

printf("\nInserting Elements at the beginning...\n"); // Getting the elements from user and inserting in newnode

newnode =(Node*) malloc(sizeof(Node));// malloc means giving intimation to preprocessor that occupies less space

printf("Address of created newnode :%d\n",newnode); // we get the address of an element from newnode and prints on screen

newnode->data = n;

printf("Inserted value is :%d\n",newnode->data); // Inserted value is displayed on screen

newnode->next = head_ref; // Process will be repeated

head_ref = newnode;

return head_ref;

}

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

{

while(head_pointer1 != 0) // Checking not equal to 0

{

head_union = insert_node(head_union,head_pointer1->data); // Pushes the value of head_pointer1 into head_union

head_pointer1 = head_pointer1->next;// For repeating the process

}

while(head_pointer2 != 0) // Checking not equal to 0

{

if(!compare(head_union,head_pointer2->data))// Comparing head_pointer2 with head_union

head_union = insert_node(head_union,head_pointer2->data); // head_union is a variabble name and stores the value of union

head_pointer2 = head_pointer2->next;// Process will be repeated

}

return head_union;

}

int compare(Node *head,int data)

{

while (head != NULL)// head values not should be NULL

{

if (head->data == data)// if both data is not equal means it goes to next data

return 1;

head = head->next;// Process will be repeated

}

return 0;

}

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

{

while (head_pointer1 != 0)// we have to check it is equal to 0

{

if (compare(head_pointer2, head_pointer1->data))// comparing both head_pointer1 and 2 and pointer2 is kept constant

head_intersection = insert_node(head_intersection,head_pointer1->data); // pushing head_pointer1 data into head intersection

head_pointer1 = head_pointer1->next;// Process will be repeated

}

return head_intersection;

}

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.