Challenge

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

Code:

#include
#include

struct node
{
	int data;
	struct node *link;
};

struct node* addr_def(struct node** head, int value);
void printlist(struct node* head);
struct node* node_intersection(struct node* head1, struct node* head2);
struct node* node_union(struct node* head1, struct node* head2);
struct node* compare(struct node* head_union, int data);

int main()
{
	int i,n1,n2;

	struct node *head1 = NULL;
	struct node *head2 = NULL;
	struct node *res_intersection = NULL;
	struct node *res_union = NULL;

	//Define elements of list1
	printf("\n Enter the no. of nodes in list1: ");
	scanf("%d", &n1);

	int a1[n1];
	for(i=0; i=0; i--)
	{
		addr_def(&head1, a1[i]); //forming linked list from array data
	}
	printf("\n\n Elements of list1 are: ");
	printlist(head1); //display linked list1 elements

	//Define elements of list2
	printf("\n\n Enter the no. of nodes in list2: ");
	scanf("%d", &n2);

	int a2[n2];
	for(i=0; i=0; i--)
	{
		addr_def(&head2, a2[i]); //forming linked list from array data
	}
	printf("\n Elements of list2 are: ");
	printlist(head2); //display linked list elements


	//find interaction of list1 and list2
	res_intersection = node_intersection(head1, head2);
	printf("\n Intersection of list1 and list2 is: ");
	printlist(res_intersection); //display intersection linked list elements


	//find union of list1 and list2
	res_union = node_union(head1, head2);
	printf("\n Union of list1 and list2 is: ");
	printlist(res_union); //display union linked list elements

}

//form linked list
struct node* addr_def(struct node** head, int value)
{
	struct node *newnode = (struct node*)malloc(sizeof(struct node)); //memory for new node is created
	newnode->data = value; //array value is added to new node data
	printf("\n\n Inserted new value of newnode: %d", newnode -> data);
	newnode->link = *head; //head value is inserted in new node link
	*head = newnode; //new node value is assigned to head
	return *head;
}

//print linked list
void printlist(struct node* head)
{
	struct node* tail=head; //head value is inserted in tail
	while(tail != NULL)
	{
		printf(" %d", tail -> data);
		tail = tail -> link;
	}
}

struct node* node_intersection(struct node* head1, struct node* head2)
{
	struct node *head_inters = (struct node*)malloc(sizeof(struct node));
	head_inters = NULL;

	while(head1 != NULL)
	{
		if(compare(head2, head1->data))
			head_inters = addr_def(&head_inters, head1-> data);
		head1 = head1 -> link;
	}
	return head_inters;
}

struct node* node_union(struct node* head1, struct node* head2)
{
	struct node *head_union = (struct node*)malloc(sizeof(struct node));
	head_union = NULL;

	while(head1 != NULL)
	{
		head_union = addr_def(&head_union, head1 -> data);
		head1 = head1 -> link;
	}

	while(head2 != NULL)
	{
		if(! compare(head_union, head2->data))
			head_union = addr_def(&head_union, head2-> data);
		head2 = head2 -> link;
	}
	return head_union;
}

struct node* compare(struct node* head_comp, int data)
{
	while(head_comp != NULL)
	{
		if(head_comp-> data == data)
			return head_comp;
		head_comp = head_comp->link;
	}

	return 0;
}

Output:


Enter the no. of nodes in list1: 4

Enter 1 element: 1

Enter 2 element: 2

Enter 3 element: 3

Enter 4 element: 4


Inserted new value of newnode: 4

Inserted new value of newnode: 3

Inserted new value of newnode: 2

Inserted new value of newnode: 1

Elements of list1 are: 1 2 3 4

Enter the no. of nodes in list2: 4


Enter 1 element: 2


Enter 2 element: 3


Enter 3 element: 4


Enter 4 element: 5


Inserted new value of newnode: 5

Inserted new value of newnode: 4

Inserted new value of newnode: 3

Inserted new value of newnode: 2
Elements of list2 are: 2 3 4 5

Inserted new value of newnode: 2

Inserted new value of newnode: 3

Inserted new value of newnode: 4
Intersection of list1 and list2 is: 4 3 2

Inserted new value of newnode: 1

Inserted new value of newnode: 2

Inserted new value of newnode: 3

Inserted new value of newnode: 4

Inserted new value of newnode: 5
Union of list1 and list2 is: 5 4 3 2 1

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.