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: Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.

Code for the given requirements:

#include<stdio.h>

#include<stdlib.h>

//creating a node

struct node{

int value;

struct node *next;

};

//print the linked list value

void printLinkedlist(struct node*p)

{

while(p!= NULL)

{

printf("%d",p->value);

fflush(stdout);

p=p->next;

}

}

int main()

{

//initialized nodes

struct node *one= NULL;

struct node *two= NULL;

struct node *three= NULL;

//allocate memory

one=malloc(sizeof(struct node));

two= malloc(sizeof(struct node));

three= malloc(sizeof(struct node));

//assign value values

one->value=1;

two->value=2;

three->value=3;

//connect nodes

one->next=two;

two->next=three;

three->next= NULL;

//print node-value

printf("List One\n");

fflush(stdout);

printLinkedlist(one);

//initialized nodes

struct node *four= NULL;

struct node *five= NULL;

struct node *six= NULL;

//allocate memory

four= malloc(sizeof(struct node));

five=malloc(sizeof(struct node));

six=malloc(sizeof(struct node));

//assign value values

four->value=1;

five->value=4;

six->value=5;

//connect nodes

four->next=five;

five->next=six;

six->next= NULL;

//print node-value

printf("\n List Two \n ");

fflush(stdout);

printLinkedlist(four);

printf("\n");

fflush(stdout);

printf("_________Union____________\n");

fflush(stdout);

if(one->value== four->value)

{

printf("%d\n",one->value);

fflush(stdout);

}

else

{

printf("%d\n",one->value);

fflush(stdout);

printf("%d\n",four->value);

fflush(stdout);

}

if(two->value== five->value)

{

printf("%d\n",two->value);

fflush(stdout);

}

else

{

printf("%d\n",two->value);

fflush(stdout);

printf("%d\n",five->value);

fflush(stdout);

}

if(three->value== six->value)

{

printf("%d\n",three->value);

fflush(stdout);

}

else

{

printf("%d\n",three->value);

fflush(stdout);

printf("%d\n",six->value);

fflush(stdout);

}

printf("_______Intersection____________\n");

fflush(stdout);

//intersection

struct node *temp3 = one;

struct node *temp4 = four;

while(temp3!=NULL && temp4!=NULL)

{

if(temp3->value == temp4->value)

{

printf("%d\n",temp3->value);

fflush(stdout);

temp3 = temp3->next;

temp4 = temp4->next;

}

}

}

Output:

List One

123

List Two

145

_________Union____________

1

2

4

3

5

_______Intersection____________

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.