Challenge

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

Answers

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

Overview of the project

In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists.

In this project we use two linked list which data is put by user and then we create many function to process both linked list. This function is for union operation of linked list, intersection opration of linked list and print linked list

Goals

  • Implementation of interface for mathematical Set Theory using C programming.
  • Giving two list of data as input to get the output as union & intersection for given list of data.
  • Learn various function declaration and make user define header file.
  • Learn Find and compare element of linked list nodes.

Key Highlights of the project

  • Creating two list of data using linked list method.
  • Insert the node to 1st and 2nd list accordingly.
  • Intersection of data of two lists.
  • Getting Union of two list.
  • Make header file and use it.

Deliverables of this project

  • Intersection of data of two list.
  • Union of data of two list.

Main Program

#include <stdio.h>                                                       // include header file
#include <stdlib.h>                                                      // include standard library file
#include "List.h"

int main()
{
	int i, Nodes_Linked_List_1, Nodes_Linked_List_2, temp;           // create variables
	Linked_List_1 = Linked_List_2 = NULL;                            // make linked list 1 and linked list 2 null
	printf("Create First Linked List\n");                            // creating first linked list
	printf("Enter Number Of Nodes In First Linked List: ");          // print instruction
	fflush(stdout);
	scanf("%d", &Nodes_Linked_List_1);                               // scan nodes data
	printf("\nEnter Nodes Data\n\n");                                // print instruction
	fflush(stdout);
	for(i=0; i<Nodes_Linked_List_1; i++)                             // for loop for scan all nodes data
	{
		printf("Enter %d Node data: ",i+1);
		fflush(stdout);
		scanf("%d", &temp);                                      // scan node data
		insert(&Linked_List_1, temp);                            // insert data in linked list 1
	}
	printf("\nFirst Linked List Is: ");
	printLinkedList(Linked_List_1);                                  // print linked list 1
	printf("\n\nCreate Second Linked List\n");                       // create second linked list
	printf("Enter Number Of Nodes In Second Linked List: ");         // print instruction to add data of second linked list
	fflush(stdout);
	scanf("%d", &Nodes_Linked_List_2);                               // scan node data
	printf("\nEnter Nodes Data\n\n");                                // print instruction
	fflush(stdout);
	for(i=0; i<Nodes_Linked_List_2; i++)                             // for loop for scan all nodes data
	{
		printf("Enter %d Node data: ",i+1);
		fflush(stdout);
		scanf("%d", &temp);                                      // scan node data
		insert(&Linked_List_2, temp);                            // insert data in linked list 2
	}
	printf("\nSecond Linked List Is: ");
	printLinkedList(Linked_List_2);                                  // print linked list 2
	findunion(Linked_List_1, Linked_List_2);                         // call findunion function for union operation
	intersection(Linked_List_1, Linked_List_2);                      // call intersection function for intersection operation
	printf("\n\nUnion Linked List Is: ");                            // print message
	printLinkedList(Union_Linked_List);                              // print union linked list
	printf("\n\nIntersection Linked List Is: ");                     // print message
	printLinkedList(Intersection_Linked_List);                       // print intersection linked list

	return 0;
}

Library

/*
 * List.h
 *
 *  Created on: 08-Aug-2022
 *      Author: Axit Rupareliya
 */

#ifndef LIST_H_
#define LIST_H_

struct node                                                                       // structure for linked list node
{
	int data;                                                                 // create data variable for nodes
	struct node *next;                                                        // create address variable for node
}*Linked_List_1,*Linked_List_2,*Union_Linked_List,*Intersection_Linked_List;      // variable which use structure


void insert(struct node **head, int num)                                          // function for insert node in linked list
{
	struct node* newNode = (struct node*)malloc(sizeof(struct node));         // create a memory for a new node
	newNode->data = num;                                                      // provide data for newly created node
	newNode->next = *head;                                                    // assign head pointer to next pointer of new node
	*head = newNode;                                                          // make new node as new head node of linked list
}

int search(struct node *head, int num)                                            // search element in linked list
{
	while (head != NULL)                                                      // while loop which execute code segment till address value is null
	{
		if (head->data == num)                                            // if condition for data comparison
		{
			return 1;                                                 // if node data matched it return 1
		}
	head = head->next;                                                        // compare next node data
	}
	return 0;                                                                 // while head reach null value it return 0
}

struct node* findunion(struct node *Linked_List_1, struct node *Linked_List_2)    // function for union operation of linked list
{
	Union_Linked_List = NULL;                                                 // make union linked list NULL
	struct node *temp = Linked_List_1;                                        // store address of first linked list in temp
	while(temp != NULL)                                                       // while loop which execute code segment till null value is achived
	{
		insert(&Union_Linked_List, temp->data);                           // insert linked list 1 to union linked list
		temp = temp->next;                                                // change address of temp to next node address
	}
	while(Linked_List_2 != NULL)                                              // insert node of linked list 2 which not available on linked list 1
	{
		if(!search(Linked_List_1, Linked_List_2->data))                   // if data of node is not available in linked list 1 this node is add to union linked list
		{
			insert(&Union_Linked_List, Linked_List_2->data);          // insert node to union linked list
		}
		Linked_List_2 = Linked_List_2->next;                              // check next node of linked list
	}
	return Union_Linked_List;                                                 // return with union linked list
}

struct node* intersection(struct node *Linked_List_1, struct node *Linked_List_2) // function for intersection operation of linked list
{
	Intersection_Linked_List = NULL;                                          // make intersection linked list as null
	while(Linked_List_1 != NULL)                                              // check linked list 1 is null or not
	{
		if(search(Linked_List_2, Linked_List_1->data))                    // if data of node is not available in linked list 1 this node is add to intersection linked list
		{
			insert(&Intersection_Linked_List, Linked_List_1->data);   // insert node to union linked list
		}
		Linked_List_1 = Linked_List_1->next;                              // check next node of linked list
	}
	return Intersection_Linked_List;                                          // return with union linked list
}

void printLinkedList(struct node *nodePtr)                                        // function for print linked list data
{
	while (nodePtr != NULL)                                                   // while loop for execute code segment till null address is reach
	{
		printf("%d  ", nodePtr->data);                                    // print data of node
		nodePtr = nodePtr->next;                                          // move to next position
	}
}


#endif /* LIST_H_ */

Output

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.