Challenge

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

/*code starts*/

#include
#include
#include
//step 1 create a node:
typedef struct node //defininf thestructure
{
int data;// creating thedata type of the vaiable
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 different fuction with the argumetns

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 display the nodes data
{
if (head_ref == 0)
{
printf("List is empty.");
}
else
{
tail = head_ref; //transfering address if the list to tail
while(tail !=0)
{
printf("%d", tail->data);//prints the data of current node
tail = tail->next; // advance 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");
newnode = (Node*)malloc(sizeof(Node)); //allocating the memory to the nodes
printf("Address of created nwenode : %d \n",newnode);
newnode -> data = n;//assigning the value to node
printf("Inserted value is : %d \n",newnode->data);
newnode -> next = head_ref;//address of the previous 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) //2 3 4 5
{
head_union = insert_node(head_union,head_pointer1->data);//inserting the data of the list 1 into union fuction
head_pointer1 = head_pointer1->next; // transfer the node using the address in next
}
while(head_pointer2 !=0)//2 3 4 5 6
{
if(!compare(head_union,head_pointer2->data))//iserting the element from list 2 number
head_union = insert_node(head_union,head_pointer2->data);//inserting the element not in list 2
head_pointer2 = head_pointer2->next; //Travelling to next node
}
return head_union; //2 3 4 5 6
}

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)//2 3 4 5
{
if( compare(head_pointer2,head_pointer1->data))//comparing the both list for intersection
head_intersection = insert_node(head_intersection, head_pointer1-> data);//transferring the that value to the intersection
head_pointer1 = head_pointer1->next; //Travelling to next node
}
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 the elements in the List
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 secon 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 run time
head2 = insert_node(head2, second_list_items);//inserting the data in List
}
display_list(head2);//displaying the head2 List

res_union=get_union(head1,head2);//transferring the resulf of union fucncton to the res union
printf("Here is the result of union:");
display_list(res_union);//transfering the values as arguments to display fuction

res_intersection =get_intersection(head1, head2);//transferinf the result of intersection function to res intersection
printf("Here is the result of intersection:");
display_list(res_intersection);

}

/* code ends*/ 

/* output*/

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.