Challenge

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

Overview of the Project:

The project describes the set theory using c programming. we to write the code for the intersection, union, compare and get the output. From this project were able to learn the insertion, and compare two lists. There we are going to use three files one main file , a source file, and a head file.

Goals

  • To understand how the data is inserted into the list.
  • To understand the how pointer works.

Key Highlights of the Project:

  • Inserting a node
  • union operation
  • intersection
  • Storing address in a pointer
  • Comparing the two lists

Deliverables of this project:

  • Learning about the insertion of a node.
  • How to use a pointer.
  • Calling a function
  • Struct usage.

Programs

MAIN.c file Program

Code

#include "list2.h"//header//
int main()//main function//
{
int first_list_nodes=0,second_list_nodes=0,first_list_items=0,//local variable//
second_list_items=0;
printf("\nEnter the number of nodes in first list:");//printing//
fflush(stdout);//clearing cache memory//
scanf("%d",&first_list_nodes);//storing the data//
printf("\nEnter %d the elements:",first_list_nodes);//printing//
for(int i=0;i < first_list_nodes;i++)//checking the condition//
{
fflush(stdout);//clear the cache memory//
scanf("%d",&first_list_items);//storing the data//
head1=insert_node(head1,first_list_items);//calling the function//
}
display_list(head1);//calling the function//
printf("\nEnter the number of nodes in second list:");//printing//
fflush(stdout);//clearing the cache memory//
scanf("%d",&second_list_nodes);//storing the data//
printf("\nEnter %d the elements:",second_list_nodes);//printing//
for(int i=0;i < second_list_nodes;i++)//checking the condition//
{
fflush(stdout);
scanf("%d",&second_list_items);
head2=insert_node(head2,second_list_items);
}
display_list(head2);//calling the function//
res_union = get_union(head1,head2);

display_list(res_union);
res_intersection=get_intersection(head1,head2);

printf("\nHere is the result of intersection:");//printing//
display_list(res_intersection);
}

list.c file program

Code

#include "list2.h"
void display_list(Node *head_ref)//calling function//
{
if(head_ref==0)//checking the condition//
{
printf(" list is empty");
}
else//if the above if condition does not satisfy this condition come here//
{
tail=head_ref;
while(tail !=0)
{
printf("%d", tail->data);
tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n)//calling function//
{
printf("\nInserting Element at the beginning...\n");//printing
newnode=(Node*)malloc(sizeof(Node));//node size//
printf("Address of created newnode :%d\n",newnode);//printing//
newnode->data=n;//storing the address//
printf("Inserted value is :%d\n",newnode->data);//printing//
newnode->next =head_ref;//reference node=0//
head_ref=newnode;//assigning newnode data to head ref//
return head_ref;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)//calling function//
{
while(head_pointer1 !=0)//condition checking that head pointer not equal to zero//
{
head_union= insert_node(head_union,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
while(head_pointer2 !=0)//condition checking that head pointer not equal to zero//
{
if(! compare(head_union,head_pointer2->data))
head_union=insert_node(head_union,head_pointer2->data);
head_pointer2=head_pointer2->next;
}
return head_union;
}
int compare(Node *head,int data)
{
while(head != NULL)
{
if(head->data == data)
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))
head_intersection = insert_node(head_intersection,head_pointer1->data);
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}

list.h file Program

Code

#ifndef LIST_H_
#define LIST_H_
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>


typedef struct node
{
int data;
struct node *next;
}Node;//structure declaration//
static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_intersection,*res_union;//staic variable declaration//
Node *insert_node(Node *head_ref,int n);//inseration of node//
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);//function calling//
#endif /* LIST_H_ */

Expected Outcomes:

  • Usage of data inseration
  • Usage of data pointer
  • usage of structures

Program code 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.

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.