Challenge

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

P1-Header file

#ifndef PROECT_1_SETS_H_ //To avoid error during ,multiple insertion in main file

#define PROECT_1_SETS_H_

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

typedef struct N //Node definition

{

int D;

struct N *tail;

}Nd;

static Nd *H1,*H2,*H,*H_U,*H_I,*New_N,*T,*Union,*Inter;

Nd *Insrt_Nd(Nd *Ref_H, int n); //function declarations

void Disp_List(Nd *Ref_H);

Nd *get_U(Nd *H_ptr1, Nd *H_ptr2);

Nd *get_I(Nd *H_ptr1, Nd *H_ptr2);

int cmpr(Nd *H, int D);

#endif /* PROECT_1_SETS_H_ */

P1-Source file

#include "Proect-1_Sets.h"

void Disp_List(Nd *Ref_H)

{

#ifdef DEBUG

printf("Entering to the Display function\n");

#endif

if(Ref_H==0) //If starting linked list data is zero

printf("The List is fully empty\n");

else

{

T=Ref_H; //If there is data in starting linked list store the data in tail

while(T!=0){

printf(" %d",T->D);

T=T->tail;

}

T=New_N; //If there is no data in tail store next node data

}

#ifdef DEBUG

printf("\nThe value of tail is :%d\n",T);

#endif

}

Nd *Insrt_Nd(Nd *Ref_H, int n)

{

New_N=(Nd*) malloc(sizeof(Nd));

printf("\nInserting the elements of the node at the beginning:\n");

printf("Address of newly created Node is : %d\n",New_N);

New_N->D=n;

printf("Inserted value to the created Node is: %d\n",New_N->D);

New_N->tail=Ref_H;

Ref_H=New_N;

#ifdef DEBUG

printf("Address of the Head is: %d\n",Ref_H);

printf("Address of New Node is: %d\n",New_N);

printf("Coming out of Insert function\n");

#endif

return Ref_H; }

Nd *get_U(Nd *H_ptr1, Nd *H_ptr2) {

while(H_ptr1!=0)

{

H_U=Insrt_Nd(H_U,H_ptr1->D);

H_ptr1=H_ptr1->tail;

}

while(H_ptr2 !=0)

{

if(!cmpr(H_U,H_ptr2->D))

H_U=Insrt_Nd(H_U,H_ptr2->D);

H_ptr2=H_ptr2->tail;

} return H_U; }

int cmpr(Nd *H, int D)

{

while(H!=NULL)

{

if(H->D==D)

return 1;

H=H->tail;

} return 0; }

Nd *get_I(Nd *H_ptr1, Nd *H_ptr2)

{

while(H_ptr1 !=0)

{

if(cmpr(H_ptr2, H_ptr1->D))

H_I=Insrt_Nd(H_I, H_ptr1->D);

H_ptr1=H_ptr1->tail;

} return H_I; }

P1-Main Source File

#include "Proect-1_Sets.h"

int main() {

int F_LN=0, S_LN=0, F_LI=0, S_LI=0; //Variable definition

printf("Number of Nodes required in the First List: ");

fflush(stdout);

scanf(" %d", &F_LN); //Take number of data in first list nodes from user

printf(" Enter the %d elements:",F_LN); //Print the entered data

for(int i=0;i<F_LN;i++) //Take all the data of first list node from user

{

fflush(stdout);

scanf(" %d", &F_LI);

H1=Insrt_Nd(H1, F_LI); }

Disp_List(H1); //function call

printf(" \n\nNumber of Nodes required in the Second List: ");

fflush(stdout);

scanf(" %d",&S_LN);

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

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

{

fflush(stdout);

scanf(" %d",&S_LI);

H2=Insrt_Nd(H2, S_LI); }

Disp_List(H2);

Union=get_U(H1,H2);

#ifdef DEBUG_MAIN

printf("\nHead-1 = %d and Head-2 = %d",H1,H2);

#endif

#ifdef DEBUG_MAIN

printf("\nAfter performing Union:\nHead-1 = %d and Head-2 = %d\n",H1,H2);

printf("Values of results of Union : %d\n",Union);

#endif

printf("\nResults of Union:");

Disp_List(Union);

Inter=get_I(H1,H2);

#ifdef DEBUG_MAIN

printf("\nAfter performing Intersection:\nHead-1 = %d and Head-2 = %d\n",H1,H2);

printf("Values of results of Intersection : %d\n",Inter);

#endif

printf("Results of Intersection:");

Disp_List(Inter);

}

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.