Challenge

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

Aim:

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.

1.Here we taken two sets of the data A={1,2,3,4,5} and B={2,6,5,7,1,8}

2.After that we can apply the union and intersection of these data.

Goals:

1.Implementation of interface for mathematical Set Theory using C programming.

2.Giving two list of data as input to get the output as union & intersection for given list of data.

3.Unions means it will store the all data in same location. 

4.Intersection means it will store same data of both given list.

code

#include //preprocessor header file

#include

// create Link list node

struct Node //define structure node

{

int data;

struct Node* next; // Pointer pointing towards next node

};

int i;

struct Node* createlist()

{

int n; //declaration of integer variable

struct Node *head=NULL,*p,*ptr; //structure pointer

int x,i;

printf("enter no of element in list : \n"); // writes to console

fflush(stdout); //to clear standard out console buffer

scanf("%d",&n); // reads to the console %d format specifier

for(i=0;i

{

printf("enter the data value: "); // writes to console

fflush(stdout); //to clear standard out console buffer

scanf("%d",&x); // reads to the console %d format specifier

p=(struct Node*)malloc(sizeof(struct Node));

p->data=x;

if(head == NULL)

{

head=ptr=p;

}

else

{

ptr->next=p;

ptr=p;

}

}

return head;

}

//structure to print the linked list

void displaylist(struct Node *list)

{

struct Node *p;

p=list;

while(p != 0)

{

printf("%d",p->data); //print the data of current list

fflush(stdout);

p=p->next; //advance the position of current node

}

}

// Function to get intersection of two linked list list1 &l list2

int intersection(struct Node *list1,struct Node *list2)

{

printf("\nIntersection list:\t"); //writes to console

fflush(stdout);

struct Node *temp3=list1;

struct Node *temp4=list2;

while(temp3)

{

while(temp4)

{

if(temp3->data == temp4->data) //if temp3 is equal to temp4, data temp3 is next node

{

printf("%d\t",temp3->data);

}

temp4= temp4->next;

}

temp3= temp3->next;

temp4= list2;

}

return 0;

}

//Function to get union of two linked lists list1 & list2

int unin(struct Node *list1,struct Node *list2)

{

printf("\nUnion list:\t"); //writes to console

fflush(stdout);

struct Node *temp3 = list1;

struct Node *temp4 = list2;

displaylist(temp3);

while(temp4)

{

if(temp3->data != temp4->data) //if temp3 is not equal to temp4, data temp3 is next node

{

printf("%d\t",temp4->data);

}

temp4= temp4->next;

}

return 0;

}

// Main function

int main()

{

struct Node *list1,*list2; // function calling with structure pointer

list1=list2=NULL;

//print and create the list display

printf("list1 :\n");

list1=createlist();

printf("list2 :\n");

list2=createlist();

printf("\nlist1 is: ");

displaylist(list1); //display list1

printf("\nlist2 is: ");

displaylist(list2); //display list2

intersection(list1,list2);

unin(list1,list2);

}

output

list1 :
enter no of element in list :
5
enter the data value: 1
enter the data value: 2
enter the data value: 3
enter the data value: 4
enter the data value: 5
list2 :
enter no of element in list :
6
enter the data value: 5
enter the data value: 6
enter the data value: 1
enter the data value: 2
enter the data value: 3
enter the data value: 4

list1 is: 12345
list2 is: 561234
Intersection list: 1 2 3 4 5
Union list: 123455 6 2 3 4

over view and explanation of this in step wise

  1. The preprocessor directive #include includes the standard input/output header file, which contains function declarations for input/output functions such as printf() and scanf().

  2. The preprocessor directive #include includes the standard library header file, which contains function declarations for functions such as malloc() and free().

  3. The struct Node definition creates a new data type called struct Node, which represents a node in a linked list. It has two fields: an integer data and a pointer next to a struct Node.

  4. The createlist() function is defined. It prompts the user to enter the number of elements in the linked list and reads in this value. Then, it reads in the data values for each element from the user, creates a new node for each element using malloc(), and adds the node to the linked list. It returns a pointer to the head of the linked list.

  5. The displaylist(struct Node *list) function is defined. It takes a pointer to the head of a linked list as an argument and prints out the data values of all the nodes in the list.

  6. The intersection(struct Node *list1,struct Node *list2) function is defined. It takes two pointers to the heads of two linked lists as arguments and prints out the data values that are common to both lists. It does this by iterating over both lists and comparing the data values of the nodes.

  7. The unin(struct Node *list1,struct Node *list2) function is defined. It takes two pointers to the heads of two linked lists as arguments and prints out all the data values in the two lists, including duplicates. It does this by first printing out all the nodes in the first list, then iterating over the second list and printing out any data values that are not already present in the first list.

  8. The main() function is defined. It creates two linked lists by calling the createlist() function and stores the pointers to the heads of the lists in variables list1 and list2. It then displays both lists using the displaylist() function. Finally, it calls the intersection() and unin() functions to compute and print the intersection and union of the two lists, respectively.

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.