Challenge

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

/****************************************************************************************
 * FILE NAME 	: setslinked.c
 * Created on 	: 30-Oct-2022
 * Author 		: sarath
 * DESCRIPTION 	:CREATION OF USER DEFINED DATATYPE USING LINKED LIST FOR SET THEORY
 *****************************************************************************************/
#include
#include
struct node{									//structure declaration and definition for node
	int num;
	struct node *nxtptr;
}*stnode,*a,*b,unnode,*tmp1,*tmp2,*tmp,*innode,*innode1;

void createlist(int n);							//function declaration for creating a node
void displaylist(struct node*stnode);			//function declaration for displaying list
void union_list(struct node *a, struct node *b);	//function declaration for union list calling
void intersect_list(struct node *a, struct node *b);	//function declaration for intersection list calling

int main()
{
	int n;
	printf("creating and printing linked list");
	printf("\nenter number of nodes");
	fflush(stdout);
	scanf("%d",&n);									//reading number of nodes
	createlist(n);
	a=stnode;
	printf("\ndata entered in the list:");
	displaylist(a);
	createlist(n);
	b=stnode;
	printf("\ndata entered in the list:");
	displaylist(b);
	union_list(a,b);
	intersect_list(a,b);
	return 0;
}

void createlist(int n)
{
	int num,i;
	struct node *fnnode,*tmp;
	stnode=(struct node*)(malloc(sizeof(struct node)));		//memory alloaction for new node
	if(stnode==NULL)										//checking memory allocated or not
	{
		printf("\nno data to enter as no memory to allocate");
	}
	else
	{
		printf("\n enter input data for node 1:");
		fflush(stdout);
		scanf("%d",&num);
		stnode->num=num;									//entering data to node
		stnode->nxtptr=NULL;
		tmp=stnode;
		for(i=2;i<=n;i++)
		{
			fnnode=(struct node*)(malloc(sizeof(struct node)));		//creating a node by allocating memory to it containing number and address of next
			if(fnnode==NULL)
			{
				printf("\nno data to enter as no memory to allocate");
			}
			else
			{
				printf("\n enter input data for node %d:",i);
				fflush(stdout);
				scanf("%d",&num);
				fnnode->num=num;
				fnnode->nxtptr=NULL;
				tmp->nxtptr=fnnode;		//linking previous adress i.e tmp to next adress
				tmp=tmp->nxtptr;
			}
		}
	}
}
void displaylist(struct node *stnode)
{
	 struct node *tmp;
	 if(stnode==NULL)
	 {
		 printf("\n list is empty");
	 }
	 else
	 {
		 tmp=stnode;
		 while(tmp!=NULL)
		 {
			 printf("\n data :%d",tmp->num);	//print data of current node
			 tmp=tmp->nxtptr;					// point to next address
		 }
	 }
}
void union_list(struct node *a, struct node *b)
{
	int check=0;							//FOR USE IN CHECKING WHETHER ELENT OF 2ND LIST EXIST IN LIST 1
	struct node* tmp1;
	struct node* tmp2;
	tmp1=a;
	tmp2=b;
	unnode.num=a->num;
	unnode.nxtptr=NULL;
	tmp=&unnode;							//UNNODE IS CREATED TO NOT ALTER a WE CAN USE A ALSO BUT A WILL ALTER SO TO PRESERVE A IM USING UNNODE
	while(tmp1->nxtptr!=NULL)
	{
		tmp1=tmp1->nxtptr;
		struct node *dummy=(struct node *)malloc(sizeof(struct node));		//CREATING A DUMMY NODE TO ADD UNION ELEMENTS
		dummy->num=tmp1->num;
		dummy->nxtptr=NULL;
		tmp->nxtptr=dummy;
		tmp=tmp->nxtptr;
	}
	tmp1=&unnode;							//ASSIGNING UNNODE TO TEMP SO THAT DATA IN UNNODE IS PRESERVED BY DOING ALTERATIONS ON TEMP
	while(tmp2)								//LOOP FOR CHECKING EVERY ELEMENT OF 2ND LINKED LIST WITH 1
	{
		check=0;
		while(tmp1)							//LOOP FOR ITERATING ALL 2ND LIST ELEMENTS TO COMPRE WITH FIRST LIST
		{
			if(tmp2->num == tmp1->num)
			{
				check=1;					//IF EQUAL TO 1 THEN I DONT NEED TO COPY AS I ALREADY COPIED ELEMENTS IN A
			}
			tmp=tmp1;
			tmp1=tmp1->nxtptr;
		}
		if(check==0)						// ADDING ELEMENT IF ITS NOT IN LIST 1
		{
			struct node *dummy=(struct node *)malloc(sizeof(struct node));	//CREATING A DUMMY NODE TO ADD UNION ELEMENTS
			dummy->num = tmp2->num;
			dummy->nxtptr = NULL;
			tmp->nxtptr=dummy;
		}
		tmp1=&unnode;
		tmp2=tmp2->nxtptr;
	}
	printf("\nunion of linked list is:");
	displaylist(&unnode);								//DISPLAYING LIST ELEMENTS OF UNION NODE
}
void intersect_list(struct node *a, struct node *b)
{
	struct node *innode1,*tmp3;
	struct node *innode=(struct node *)malloc(sizeof(struct node));
	tmp1=a;
	tmp2=b;
	int c=0;
	while(tmp1)										//CHECKING LOOP ELENTS TO COMPARE TILL NULL IN LIST 1
	{
		while(tmp2)									//CHECKING LOOP ELENTS TO COMPARE TILL NULL IN LIST 2
		{
			if(tmp1->num == tmp2->num)				//CHECKING FOR COMMON ELEMENTS IN 2 LISTS
			{
				if(c==0)							//FOR CREATING FIRST NODE ELEMENT
				{
					innode->num=tmp1->num;
					innode->nxtptr=NULL;
					tmp3=innode;
					c=1;
				}
				else								// FOR ADDING ELEMENTS FROM 2ND NODE
				{
					innode1=(struct node *)malloc(sizeof(struct node));
					innode1->num=tmp1->num;
					innode1->nxtptr=NULL;
					tmp3->nxtptr=innode1;			//LINKING FIRST NODE TO PREVIOUS NODE
					tmp3=tmp3->nxtptr;
				}
			}
			tmp2=tmp2->nxtptr;
		}
		tmp1=tmp1->nxtptr;
		tmp2=b;
	}
	printf("\nintersection of linked list is:");
	displaylist(innode);							//DISPLAYING LIST ELEMENTS OF INTERSECTION NODE
}
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.