Challenge

Project 1 - 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, mathematical Set Theory are implemented using C programming language. User can give the input like size of two lists followed by data of lists and this program will give the output as new two lists as intersection and union of user given two lists.

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.
  • Performing node creation and creation of linked lists.

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.

Deliverables of this project

  • Displaying created two lists
  • Intersection of data of two list.
  • Union of data of two list

Outcome:

two lists created for example:

List-1: {2,3,4,5,6}, size=5

List-2: {2,4,6,8}, size=4

Intersection: {2,4,6}- common numbers of two lists

Union: {2,3,4,5,6,8}- all members of two lists

Code

#include 
#include 

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

// function for print the linked list value
void printLinkedlist(struct node *p)
{
  while (p != NULL)
  {
    printf("\n%d ", p->data);
    p = p->next;
  }
}

int main() //main function
{
	
	int j,a,b,n1,n2; //declaring variables required
	/* Initialize nodes for lists*/
	
	struct node *head1;
	struct node *L1 = NULL;
	
	struct node *head2;
	struct node *L2 = NULL;

	L1 = malloc(sizeof(struct node));
	head1 = L1;
	//seeking size of list-1 from user

	printf("Enter Size of List-1\t"); 
	scanf("%d", &n1);
	fflush(stdout);

    printf("\nEnter %d numbers: \n", n1); 
    scanf("%d", &a);
    fflush(stdout);
    
    //code for creating list-1 by user inpuut values
    L1->data = a;
	for( j=1;j<n1;j++)
	{
		
		L1->next = malloc(sizeof(struct node));
		L1 = L1->next;
		scanf("%d", &a);
		fflush(stdout);
		L1->data = a;
	}

	
	L2 = malloc(sizeof(struct node));
	head2 = L2;
	
	//seeking size of list-2 from user
	printf("\nEnter Size of List-2\t"); 
	scanf("%d", &n2);
	fflush(stdout);

    printf("\nEnter %d numbers: \n", n2);
    scanf("%d", &b);
    fflush(stdout);
    
    //code for creating list-1 by user inpuut values
    L2->data = b;
	for(j=1;j<n2;j++)
	{
		
		L2->next = malloc(sizeof(struct node));
		L2 = L2->next;
		scanf("%d", &b);
		fflush(stdout);
		L2->data = b;
	}
	// displaying list-1 & 2
	printf("\nList-1: ");
	printLinkedlist(head1);
	printf("\nList-2: ");
	printLinkedlist(head2);
	
	//creating nod and new list for intersection
	struct node *head3;
	struct node *I = NULL;
	I = malloc(sizeof(struct node));
	head3 = I;
	
	//code for finding intersection of two list L1 & L2 and store it into list-I
	
	L1 = head1;
	L2 = head2;
	
    while (L1 != NULL)
	{
	    while (L2 != NULL)
	    {
	        if(L1->data == L2->data)
	        {
	           I->data = L2->data;
	           if(L1->next != NULL && L2->next != NULL)
	           {
	           I->next = malloc(sizeof(struct node));
	           I = I->next;
	           }
	        }
	        L2 = L2->next;
	    }
	    L2 = head2;
	    L1 = L1->next;
	}
	//displaying intersection list-I
	printf("\nIntersection List: ");
	printLinkedlist(head3);
	
	//creating new node and list for union 
	struct node *head4;
	struct node *U = NULL;
	U = malloc(sizeof(struct node));
	head4 = U;
	
	//code for finding union of two list L1 & L2 and store it into list-U
	L1 = head1;
	
	do
	{
	    U->data = L1->data;
	    U->next = malloc(sizeof(struct node));
	    U = U->next;
	    L1 = L1->next;
	}while (L1 != NULL);

    L2 = head2;

	do
	{
	    U->data = L2->data;
	    if (L2->next != NULL)
	    {
	    U->next = malloc(sizeof(struct node));
	    U = U->next;
	    }
	    L2 = L2->next;
	}while (L2 != NULL);
	
	
	printf("\nUnion List: ");
	printLinkedlist(head4);
}

Output

[?2004l
Enter Size of List-1	5

Enter 5 numbers: 
2
3
4
5
6

Enter Size of List-2	4

Enter 4 numbers: 
2
4
6
8

List-1: 
2 
3 
4 
5 
6 
List-2: 
2 
4 
6 
8 
Intersection List: 
2 
4 
6 
Union List: 
2 
3 
4 
5 
6 
2 
4 
6 
8
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.