Challenge

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

Project 1

Embedded system

  1. No. 1 Create following interfaces for performing operations on two sets according to mathematical set theory (using linked list)

1.insert_node 2.display_list

3.get_union 4.get_intersection

5.compare.

ANS: Main function in project.c file

#include "operation.h"//new header file for operation

int main()//main function//calling function the function should return an integer value

{

int FLN = 0, SLN = 0, FLI = 0, SLI = 0;//integer FLN, SLN, FLI, SLI = 0

printf("Enter the total node of first list");//printing the statement

fflush(stdout);////to use for immediate flush out the contents of output stream

scanf("%d", &FLN);//taking inputs from the user for FLN

int i;

for(i=0; i<FLN; i++)//used to repeat the statement with specified number of times

{

printf("Enter the items in first list");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d", &FLI);//taking inputs from the user for FLI

head1=insert_node(head1, FLI);

}

display_list(head1);

printf("Enter the total node of second list");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d", &SLN);//taking inputs from the user for SLN

for(i=0; i<SLN; i++)

{

printf("Enter the items in second list");//printing the statement

fflush(stdout);//to use for immediate flush out the contents of output stream

scanf("%d", &SLI);//taking inputs from the user for SLI

head2=insert_node(head2, SLI);

}

display_list(head2);

res_union=get_union(head1, head2);

printf("the result of union is: ");//printing the statement

display_list(res_union);

res_intersection=get_intersection(head1, head2);

printf("the result of intersection is: ");//printing the statement

display_list(res_intersection);

}

Main header file for operation file

#ifndef OPERATION_H_

#define OPERATION_H_

#include<stdio.h>//preprocessor section

#include<stdlib.h>//header file for standard library- functions involving memory allocation and conversion

typedef struct node//allow users to provide alternative names for the primitive.

{

int item;//item holding some integer value

struct node *next;//pointer to an element of a struct node

}NODE;

//static is a function that has a scope that is limited to its object file.

static NODE *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection,*head_ref;

NODE *insert_node(NODE *head_ref, int n);//inserting new node with int n

void display_list(NODE *head_ref);//function displaying the list

NODE *get_union(NODE *head_pointer1,NODE *head_pointer2);//union can hold only one member value at a time.

NODE *get_intersection(NODE *head_pointer1,NODE *head_pointer2);//set of common element

int compare(NODE *head, int data);//comparing the entered node and replacing the new one or keeping same one(if any)

#endif /* OPERATION_H_ */

Main C file for operation file

#include "operation.h"//new header file for operation

NODE *insert_node(NODE *head_ref, int n)//function call for inserting new node

{

printf("\n Inserting elements at the beginning..\n");//printing the statement

newnode = (NODE*)malloc(sizeof(NODE));//malloc allocates size of node, and returns a void pointer to it.

printf("Address of created newnode : %d\n", newnode);//printing the statement

newnode->item = n;//now new node storing in item which is equal to n

printf("Inserted value is : %d\n", newnode->item);//printing the statement

newnode->next = head_ref;

head_ref = newnode;

return head_ref;//returning to head_ref

}

void display_list(NODE *head_ref)//function call for display list

{

if(head_ref == 0)//used to decide whether a certain statement will be executed or not

printf("List is empty\n");//printing the statement

else//used to specify a block of code to be executed, if the same condition is false

{

tail = head_ref;

while(tail != 0)//repeat a statement until a specified expression becomes false

{

printf("%d", tail->item);//printing tail->item value

tail = tail->next;

}

tail = newnode;

}

}

NODE *get_union(NODE *head_pointer1,NODE *head_pointer2)//function call for get union

{

while(head_pointer1 != 0)//repeat a statement until a specified expression becomes false

{

head_union = insert_node(head_union,head_pointer1->item);

head_pointer1 = head_pointer1->next;

}

while(head_pointer2 != 0)//repeat a statement until a specified expression becomes false

{

if(!compare(head_union,head_pointer2->item))//used to decide whether a certain statement will be executed or not

head_union = insert_node(head_union, head_pointer2->item);

head_pointer2 = head_pointer2->next;

}

return head_union;//returning to head_union

}

int compare(NODE *head, int item)//function call for comparison

{

while(head != NULL)//repeat a statement until a specified expression becomes false

{

if(head->item == item)//used to decide whether a certain statement will be executed or not

return 1;//returning to 1

head = head->next;

}

return 0;//execution successfully

}

NODE *get_intersection(NODE *head_pointer1, NODE *head_pointer2)//function call for intersection

{

while(head_pointer1 != 0)//repeat a statement until a specified expression becomes false

{

if(compare(head_pointer2, head_pointer1->item))//used to decide whether a certain statement will be executed or not

head_intersection = insert_node(head_intersection,head_pointer1->item);

head_pointer1 = head_pointer1->next;

}

return head_intersection;//returning to head_intersection

}

Output:

Enter the total node of first list4

Enter the items in first list1

Inserting elements at the beginning..

Address of created newnode : 6582208

Inserted value is : 1

Enter the items in first list2

Inserting elements at the beginning..

Address of created newnode : 6582240

Inserted value is : 2

Enter the items in first list3

Inserting elements at the beginning..

Address of created newnode : 6582272

Inserted value is : 3

Enter the items in first list4

Inserting elements at the beginning..

Address of created newnode : 6582304

Inserted value is : 4

4321Enter the total node of second list5

Enter the items in second list5

Inserting elements at the beginning..

Address of created newnode : 6582336

Inserted value is : 5

Enter the items in second list6

Inserting elements at the beginning..

Address of created newnode : 6582368

Inserted value is : 6

Enter the items in second list7

Inserting elements at the beginning..

Address of created newnode : 6582400

Inserted value is : 7

Enter the items in second list2

Inserting elements at the beginning..

Address of created newnode : 6582432

Inserted value is : 2

Enter the items in second list8

Inserting elements at the beginning..

Address of created newnode : 6582464

Inserted value is : 8

82765

Inserting elements at the beginning..

Address of created newnode : 6582496

Inserted value is : 4

Inserting elements at the beginning..

Address of created newnode : 6582528

Inserted value is : 3

Inserting elements at the beginning..

Address of created newnode : 6582560

Inserted value is : 2

Inserting elements at the beginning..

Address of created newnode : 6582592

Inserted value is : 1

Inserting elements at the beginning..

Address of created newnode : 6582624

Inserted value is : 8

Inserting elements at the beginning..

Address of created newnode : 6582656

Inserted value is : 7

Inserting elements at the beginning..

Address of created newnode : 6583312

Inserted value is : 6

Inserting elements at the beginning..

Address of created newnode : 6582896

Inserted value is : 5

the result of union is: 56781234

Inserting elements at the beginning..

Address of created newnode : 6582928

Inserted value is : 2

the result of intersection is: 2

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.