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 OF THE PROJECT :

Implementation of Interfaces for mathematical set theory in c language and user can give input as two list of data and program should give the union and the intersection of the both the lists.

OBJECTIVE :

The c program is created for the given requirement the program takes two input from the user and after taking the inputs then the user it displays the output in the console. Here we created a two linked lists to store the data of the user input and function is performed based on the given input.

STEP 1:

Opening a c file in stm cube ide and create a source file for writing the program

STEP 2:

Two header files are used which are for input and output functions and for allocating memory.

#include<stdio.h>

#include<stdlib.h>

STEP 3:

create a structure node and write the function to create a new node and insert function to insert the newly created element to the existing node. New node will be inserted before the existing or current node and head points to new node.

struct Node {

int data;

struct Node* next;

};

// creating a node

struct Node* createNode(int data){

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

newNode->data = data;

newNode->next = NULL ;

return newNode;

}

//inserting new node to existing node

void insert (struct Node** head, int data){

struct Node* newNode = createNode(data);

newNode->next = *head ;

*head = newNode;

}

STEP 4:

now create a function ifpresent to check whether the the data is present in the list and to avoid the duplicate element in the list. for ex if we have list of {2,4,3,4,6}. here there are two 4's in the list. to avoid that during the interface of sets this ifpresent is used.

int ifpresent(struct Node* head, int value){

struct Node* current = head;

while(current != NULL){

if (current->data == value){

return 1;

}

current = current -> next;

}

return 0;

}

STEP 5:

function to print the output generated by the functions and this is named as printlist and return type is void because it has no return type first it takes the data from a list and it will be assumed as head and printis the value and the while loop iterates till the value becomes null.

void printlist(struct Node* head){

struct Node* current = head;

while(current != NULL){

printf("%d ",current->data);

current = current-> next;

}

printf("\n");

}

STEP 6:

It is the function to find the union in the given list, this function also has while condition which iterates for the condition till the value becomes null. first the result is initialized and then the list 1 data points to the result by a condition of ifpresent to avoid the duplication and these are stored in the list named result and again a current pointer stores the value of head2 and check with the list 1 one by one ifpresent it will not insert and if not it will and return result.

struct Node* findunion(struct Node* list1, struct Node* list2){

struct Node* result = NULL ;

struct Node* current = list1 ;

while(current != NULL){

if(!ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

current =list2;

while(current != NULL){

if(!ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

return result;

}

STEP 7:

Next is to write function for intersection here also same as union two pointers current and result are declared and current holds the list 1 and using ifpresent condition the head2 is checked with the current value which id head1 and ifpresent it insert the value and again && !if present is used to check whether the values are repeating if the value is present in the result it will not insert else iw will insert to avoid duplication.

struct Node* findintersection(struct Node* list1, struct Node* list2){

struct Node* result = NULL ;

struct Node* current = list1 ;

while(current != NULL){

if (ifpresent(list2, current->data) && !ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

return result;

}

STEP 8:

this is the main function of the program where four struct nodes are initialised with null in which two are the names of the lists and two are the functions and the input is taken using the scanf and fflush (stdout) is used to flush the content of output and functions are called unionlist and intersectionlist and the output is printed using the printed list.

int main (){

struct Node * list1 = NULL;

struct Node * list2 = NULL;

struct Node * unionlist = NULL;

struct Node * intersectionlist = NULL;

int n,m,data;

printf("enter number of elements in list1 :");

fflush(stdout);

scanf("%d",&n);

printf("enter elements in list1 :\n");

for (int i=0; i<n; i++){

fflush(stdout);

scanf("%d", &data);

insert(&list1, data);

}

printf("enter number of elements in list2 :");

fflush(stdout);

scanf("%d",&m);

printf("enter elements in list2 :\n");

for (int i=0; i<m; i++){

fflush(stdout);

scanf("%d", &data);

insert(&list2, data);

}

unionlist = findunion(list1, list2);

intersectionlist = findintersection(list1,list2);

printf("elements of list1 are :");

printlist(list1);

printf("elements of list2 are :");

printlist(list2);

printf("elements after union of both lists are :");

printlist(unionlist);

printf("elements after intersection of both lists are :");

printlist(intersectionlist);

return 0;

}

FULL CODE :

#include<stdio.h>

#include<stdlib.h>

struct Node {

int data;

struct Node* next;

};

// creating a node

struct Node* createNode(int data){

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

newNode->data = data;

newNode->next = NULL ;

return newNode;

}

//inserting new node to existing node

void insert (struct Node** head, int data){

struct Node* newNode = createNode(data);

newNode->next = *head ;

*head = newNode;

}

//ifpresent function to avoid duplicate and for set theory

int ifpresent(struct Node* head, int value){

struct Node* current = head;

while(current != NULL){

if (current->data == value){

return 1;

}

current = current -> next;

}

return 0;

}

void printlist(struct Node* head){

struct Node* current = head;

while(current != NULL){

printf("%d ",current->data);

current = current-> next;

}

printf("\n");

}

struct Node* findunion(struct Node* list1, struct Node* list2){

struct Node* result = NULL ;

struct Node* current = list1 ;

while(current != NULL){

if(!ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

current =list2;

while(current != NULL){

if(!ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

return result;

}

struct Node* findintersection(struct Node* list1, struct Node* list2){

struct Node* result = NULL ;

struct Node* current = list1 ;

while(current != NULL){

if (ifpresent(list2, current->data) && !ifpresent(result, current->data)){

insert(&result, current->data);

}

current = current-> next;

}

return result;

}

int main (){

struct Node * list1 = NULL;

struct Node * list2 = NULL;

struct Node * unionlist = NULL;

struct Node * intersectionlist = NULL;

int n,m,data;

printf("enter number of elements in list1 :");

fflush(stdout);

scanf("%d",&n);

printf("enter elements in list1 :\n");

for (int i=0; i<n; i++){

fflush(stdout);

scanf("%d", &data);

insert(&list1, data);

}

printf("enter number of elements in list2 :");

fflush(stdout);

scanf("%d",&m);

printf("enter elements in list2 :\n");

for (int i=0; i<m; i++){

fflush(stdout);

scanf("%d", &data);

insert(&list2, data);

}

unionlist = findunion(list1, list2);

intersectionlist = findintersection(list1,list2);

printf("elements of list1 are :");

printlist(list1);

printf("elements of list2 are :");

printlist(list2);

printf("elements after union of both lists are :");

printlist(unionlist);

printf("elements after intersection of both lists are :");

printlist(intersectionlist);

return 0;

}

SCREENSHOT OF THE CODE :

RESULT :

The program is built with zero errors and executed in stm cube ide and given input for the two lists {4,4,5,6,6,5} and other list is{3,4,5,3,6} and the result obtained in the console is union of both the lists are {3,4,6,5} and the intersection of both the lists are {4,6,5}and the screenshot is pasted below and .c file is attached.

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.