Challenge

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

code for union and intersection of twp lists (list 1) and (list 2):

program:

#include<stdio.h> //header file

#include<stdlib.h> //header file

struct node //creation of user defined struction 'node'

{

int data;

struct node *address;

};

struct node *first1; //declaration global struct variable

struct node *first2; //declaration global struct variable

void linked_list(struct node* first1) //caller function to the main to display the list

{

struct node *p=first1; //Declaration of struct variable '*p'

while(p != NULL) //while loop to display list

{

printf("%d\n", p->data);

p=p->address;

}

printf("\n");

}

//caller function to the main to display the 'unions' in the list

void union_list(struct node *first1,struct node*first2)

{

int u;

struct node *q=first2; //Declaration and initialization of struct variable '*q' with first2

struct node *p=first1; //Declaration and initialization of struct variable '*p' with first1

while(p!=NULL)

{

printf("%d\n",p->data); //while loop to display list 1

p=p->address;

}

while(q != NULL) //while loop for 2nd list

{

struct node *r=first1; //Declaration and initialization of struct variable '*r' with first1

while(r != NULL) //while loop for 1st list

{

if (q->data != r->data) //if condition to check value of q not equal to r

{ //if condition true

u=r->data; //assigning value to u

r=r->address; //assigning r,address to r

}

else if(q->data == r->data) //else if condition to check value of q equal to r

{ //if condition true

u=r->data; //assigning value to u

r=NULL; //'r' to be NULL

}

}

if((q->data != u) && (r == NULL)) //if condition for q not-equal to r also r to be NULL

{

printf("%d\n",q->data);

}

q=q->address; //Assigning q,address to q

}

printf("\n");

}

//caller function to the main to display the 'intersection' values in the list

void intersection_list(struct node *first1,struct node*first2)

{

struct node *q=first2; //Declaration and initialization of struct variable '*q' with first2

while(q != NULL) //loop for the 2nd list

{

struct node *p=first1; //Declaration and initialization of struct variable '*p' with first1

while(p != NULL) //loop for the 1st list

{

if(q->data == p->data) //condition for value of q=p

{

printf("%d\n", q->data); //if true write value to console

}

p=p->address;

}

q=q->address;

}

printf("\n");

}

void list1(int n) //caller to main to insert values to list 1

{

int m;

struct node *newnode1; //declareation of struct variable

struct node *tempnode1; //declareation of struct variable

first1= (struct node*) malloc (sizeof(struct node) ); //memory allocation to first1

printf("node 1 value"); // to read value from console

fflush(stdout);

scanf("%d",&m); //Assign value to 'm'

first1->data= m;

first1->address=NULL;

tempnode1=first1; //assigning first1 address to tempnode1

for(int i=2;i<=n;i++) //loop to insert values to list 1

{

newnode1=(struct node*) malloc (sizeof(struct node) ); //allocation of memory to newnode1

printf("node %d value",i); //read value from console

fflush(stdout);

scanf("%d",&m); //assign value to 'm'

newnode1->data=m;

newnode1->address=NULL;

tempnode1->address=newnode1; //assigning newnode1 address to tempnode1->address

tempnode1=tempnode1->address; //assigning tempnode1->address to tempnode1

}

}

void list2(int o) //caller to main to insert values to list 2

{

int m;

struct node *newnode2; //declareation of struct variable

struct node *tempnode2; //declareation of struct variable

first2= (struct node*) malloc (sizeof(struct node) ); //memory allocation to first2

printf("node 1 value"); // to read value from console

fflush(stdout);

scanf("%d",&m); //Assign value to 'm'

first2->data= m;

first2->address=NULL;

tempnode2=first2; //assigning first2 address to tempnode2

for(int i=2;i<=o;i++) //loop to insert values to list 2

{

newnode2= (struct node*) malloc (sizeof(struct node) ); //allocation of memory to newnode2

printf("node %d value",i); //read value from console

fflush(stdout);

scanf("%d",&m); //assign value to 'm'

newnode2->data=m;

newnode2->address=NULL;

tempnode2->address=newnode2; //assigning newnode2 address to tempnode2->address

tempnode2=tempnode2->address; //assigning tempnode2->address to tempnode2

}

}

int main() //main function

{

int n=0,o=0; //declaration and initialization to integer variables

printf("enter no of nodes in the list 1\n");//to read nodes from console

fflush(stdout);

scanf("%d",&n); //assigning no of node to console

list1(n); //called function to insert list1

printf("enter no of nodes in the list 2\n"); //to read nodes from console

fflush(stdout);

scanf("%d",&o); //assigning no of node to console

list2(o); //called function to insert list1

printf(" list is\n");

linked_list(first1); //called function to display list 1

printf(" list is \n");

linked_list(first2); //called function to display list 2

printf("union list\n");

union_list(first1,first2); //called function to display union list

printf("intersection list\n");

intersection_list(first1,first2); //called function to display insertion list

return 0;

}

#include<stdio.h>             //header file
#include<stdlib.h>            //header file

struct node                  //creation of user defined struction 'node'
{
	int data;
	struct node *address;
};

struct node *first1;                     //declaration global struct variable
struct node *first2;                     //declaration global struct variable

void linked_list(struct node* first1)    //caller function to the main to display the list
{
	struct node *p=first1;               //Declaration of struct variable '*p'

		while(p != NULL)                 //while loop to display list
		{
		    printf("%d\n", p->data);
		    p=p->address;
		}
		printf("\n");
	}

//caller function to the main to display the 'unions' in the list

void union_list(struct node *first1,struct node*first2)
{
int u;
    struct node *q=first2;               //Declaration and initialization of struct variable '*q' with first2
	struct node *p=first1;               //Declaration and initialization of struct variable '*p' with first1

	while(p!=NULL)
	{
		printf("%d\n",p->data);          //while loop to display list 1
		p=p->address;
	}

	while(q != NULL)                     //while loop for 2nd list
	{
		struct node *r=first1;           //Declaration and initialization of struct variable '*r' with first1
		while(r != NULL)                 //while loop for 1st list
		{
			if (q->data != r->data)       //if condition to check value of q not equal to r
	    	{                             //if condition true
                u=r->data;                //assigning value to u
                r=r->address;             //assigning r,address to r
			}
			else if(q->data == r->data)   //else if condition to check value of q equal to r
			{                             //if condition true
				u=r->data;                //assigning value to u
				r=NULL;                   //'r' to be NULL
			}
		}

		if((q->data != u) && (r == NULL)) //if condition for q not-equal to r also r to be NULL
		{
			printf("%d\n",q->data);
		}
	q=q->address;                        //Assigning q,address to q
	}
	printf("\n");
}

//caller function to the main to display the 'intersection' values in the list
void intersection_list(struct node *first1,struct node*first2)
{

	struct node *q=first2;               //Declaration and initialization of struct variable '*q' with first2

	while(q != NULL)                     //loop for the 2nd list
	{

		struct node *p=first1;           //Declaration and initialization of struct variable '*p' with first1

		while(p != NULL)                 //loop for the 1st list
		{
			if(q->data == p->data)       //condition for value of q=p
			{
				printf("%d\n", q->data); //if true write value to console


			}
			p=p->address;


		}
		q=q->address;
	}
	printf("\n");
}

void list1(int n)                       //caller to main to insert values to list 1
{
	int m;
	struct node *newnode1;              //declareation of struct variable
	struct node *tempnode1;             //declareation of struct variable
	first1= (struct node*) malloc (sizeof(struct node) ); //memory allocation to first1

	printf("node 1 value");             // to read value from console
	fflush(stdout);
	scanf("%d",&m);                     //Assign value to 'm'
	first1->data= m;
	first1->address=NULL;
	tempnode1=first1;                   //assigning first1 address to tempnode1
	for(int i=2;i<=n;i++)               //loop to insert values to list 1
	{
		newnode1=(struct node*) malloc (sizeof(struct node) ); //allocation of memory to newnode1
		printf("node %d value",i);      //read value from console
		fflush(stdout);
		scanf("%d",&m);                 //assign value to 'm'
        newnode1->data=m;
        newnode1->address=NULL;
        tempnode1->address=newnode1;    //assigning newnode1 address to tempnode1->address
        tempnode1=tempnode1->address;   //assigning tempnode1->address  to tempnode1
	}
}

void list2(int o)                       //caller to main to insert values to list 2
{
	int m;
	struct node *newnode2;              //declareation of struct variable
	struct node *tempnode2;             //declareation of struct variable
	first2= (struct node*) malloc (sizeof(struct node) );  //memory allocation to first2
	printf("node 1 value");             // to read value from console
	fflush(stdout);
	scanf("%d",&m);                     //Assign value to 'm'
	first2->data= m;
	first2->address=NULL;
	tempnode2=first2;                   //assigning first2 address to tempnode2
	for(int i=2;i<=o;i++)               //loop to insert values to list 2
	{
		newnode2= (struct node*) malloc (sizeof(struct node) );  //allocation of memory to newnode2

		printf("node %d value",i);      //read value from console
		fflush(stdout);
		scanf("%d",&m);                 //assign value to 'm'
        newnode2->data=m;
        newnode2->address=NULL;
        tempnode2->address=newnode2;    //assigning newnode2 address to tempnode2->address
        tempnode2=tempnode2->address;   //assigning tempnode2->address  to tempnode2
	}
}
int main()                            //main function
{
	int n=0,o=0;                      //declaration and initialization to integer variables
		printf("enter no of nodes in the list 1\n");//to read nodes from console
		fflush(stdout);
		scanf("%d",&n);               //assigning no of node to console
		list1(n);                     //called function to insert list1
		printf("enter no of nodes in the list 2\n");  //to read nodes from console
		fflush(stdout);
		scanf("%d",&o);               //assigning no of node to console
		list2(o);                     //called function to insert list1

		printf(" list is\n");
		linked_list(first1);          //called function to display list 1
		printf(" list is \n");
		linked_list(first2);          //called function to display list 2

		printf("union list\n");
		union_list(first1,first2);    //called function to display union list
		printf("intersection list\n");
		intersection_list(first1,first2);   //called function to display insertion list

		return 0;
}

the output for this union amd intersection list is.

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.