Challenge

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

OFFLINE EMBEDDED BATCH1

PROJECT 1:

//Header files
#include
#include
#include

//Declare a linklist node
struct Node {

//Initialize the structure members
int data; //Data part
struct Node *next; //Address part
};

//Declare the function prototypes
void push(struct Node** headref, int newdata);
int Present(struct Node* head, int data);

//To get a Union values by structures
struct Node* getUnion(struct Node* head1, struct Node* head2)
{

//Assigning the values of structure members
struct Node* result = NULL;
struct Node *t1 = head1,*t2 = head2;

//Checking a condition
while (t1!=NULL)
{
push(&result,t1->data);
t1=t1->next;
}

//Checking a condition
while (t2!=NULL)
{
if (!Present(result,t2->data))
{
push(&result,t2->data);
}
t2 = t2->next;
}
return result;
}

//To get a Intersection values by structure
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;

//Checking a condition for 1st dummy variable
while (t1!=NULL)
{
if (Present(head2,t1->data))
{
push(&result,t1->data);
}
t1=t1->next;
}
return result;
}

//To create a new node
void push(struct Node** headref, int newdata)
{
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = newdata;
newnode->next = *headref;
*headref=newnode;
}

// printing the linklist using function
void printList(struct Node* node)
{
while (node!=NULL)
{
printf("%d ",node->data);
node=node->next;
}
}

int Present(struct Node* head, int data)
{
struct Node* t = head;
while (t!=NULL)
{
if (t->data==data)
{
return 1;
}
t=t->next;
}
return 0;
}

//Create a main functon
int main()
{

//Declare the Local variables
int n,m,temp;

//Printing the statement
printf("enter the size of both list: ");
fflush(stdout);
scanf("%d %d",&n,&m);

//Declare the node is first null state
struct Node* head1=NULL;
struct Node* head2=NULL;
struct Node* intersecn=NULL;
struct Node* unin=NULL;
printf("nEnter the value in linklist1 :");

//for loop is used linklist1
for(int i=0;i<n;++i)
{
fflush(stdout);
scanf("%d",&temp);
push(&head1,temp);
}
printf("nEnter the value in linklist2 :");

//for loop is used linklist2
for(int i=0;i<m;++i)
{
fflush(stdout);
scanf("%d",&temp);
push(&head2,temp);
}


intersecn = getIntersection(head1, head2); //Caller function for intersection
unin = getUnion(head1, head2); //Caller function for union
printf("nFirst list is :");

//Function calling with structure pointer as a argument
printList(head1);
printf("nSecond list is :");
printList(head2);
printf("nIntersection list is :");
printList(intersecn);
printf("nUnion list is :");
printList(unin);
return 0;
}

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.