Challenge

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

1).

#include<stdio.h>
#include<stdlib.h>

struct node
{
int value;
struct node *nxt;
};
struct node *node1,*node_n,*temp,*list1,*list2,*list4; // global declaration //

struct node* createnodelist(int n) //creating function//
{

int i,c=2;
node1=(struct node*)malloc(sizeof(node1)); // allocating the memory//
if(node1!=NULL)
{
printf("\nenter value");
scanf("%d",&node1->value); // initializing the value//
node1->nxt=NULL;
printf("\n node1 value is %d",node1->value);
}
temp=node1;
for(i=2;i<=n;i++)
{


node_n=(struct node*)malloc(sizeof(node_n));
if(node_n!=NULL)
{
printf("\nenter value"); //printing the value//
scanf("%d",&node_n->value);
node_n->nxt=NULL;
printf("\n node_%d value is %d",c,node_n->value);
c++;
}
temp->nxt=node_n; // goes to next node//
temp=temp->nxt;
}
return node1;
}

void printlist() // function for printing//

{
node1,*temp;
if(node1 == NULL)
{
printf("list is empty");
}
else
{
temp=node1;
while(temp!=NULL) //running while loop//
{
printf("\nvalue is %d",temp->value);
temp = temp->nxt; //goes to next node//
}
}
}



void intersection_list() // creating intersection function//
{
struct node *n1,*n2,*new1;
n1=list1; // putting list1 in n1//
n2=list2; // putting list1 in n1//
printf("\nintersection list");
while(n1!=NULL)
{
while(n2!=NULL)
{
if(n1->value==n2->value) // equalizing values n1&n2//
{
new1=(struct node*)malloc(sizeof(new1)); // new node creating//
new1->value=n1->value;

new1->nxt=NULL;
printf("\n%d",new1->value); // printing value//
list4=new1;

}
}


}



}


void union_list()
{
struct node *newnod,*tmp1,*tmp2,*n1,*n2; //initializing values//

tmp1=list1;
newnod=(struct node*)malloc(sizeof(newnod)); //memory allocating to new node//
newnod->value=tmp1->value;
newnod->nxt=NULL;
tmp1=tmp1->nxt; // goes to next node in list1//
list4=newnod;
tmp2=list4;

printf("\n %d",newnod->value); //printing the value//
while(tmp1!=NULL)
{

newnod=(struct node*)malloc(sizeof(newnod)); //new node creating//
newnod->value=tmp1->value;
newnod->nxt=NULL;
printf("\n %d",newnod->value);
tmp1=tmp1->nxt; //goes to next node//
tmp2=newnod;
}

n1=list1;
n2=list2;
while(n1!=NULL)
{

while(n2!=NULL) // putting in while loop//
{

if(n1->value!=n2->value)
{
newnod=(struct node*)malloc(sizeof(newnod)); // allocating the momery//
newnod->value=n1->value;
newnod->nxt=NULL;
printf("\n %d",newnod->value); //printing the value//
tmp2->nxt=newnod; // goes to next node//
tmp2=newnod;
}
n2=n2->nxt;

}
n1=n1->nxt;
}

}


int main()
{
int n,newnode1,newnode2; //initializing nodes//

printf("\nlinked list: to create and display singly linked list :");
printf("\n.......................................................");
printf("\n creating list1"); // printing the values

printf("\nenter the number of nodes");
scanf("%d",&n);

list1=createnodelist(n); // function calling//
printf("\n elements of list1 values are");
printlist(); // function calling//
printf("\n creating list2");
printf("\nenter the number of nodes"); // printing the values//
scanf("%d",&n);
list2=createnodelist(n);
printf("\n elements of list2 values are");
printlist();
intersection_list(); //function calling//
union_list();

}

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.