Challenge

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

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

Head pointer it points at the starting address of list

Tall pointer: it is a temporary pointer mainly used for traversing in the list

Newnode Pointer it points to the new created node.

Implementation of functions: In this project set data-type is implemented using linked list.

insert_node() will insert an element to the list

⚫display_list() will display all the elements pree-sent in the list. get_union() will return a list after performing union.

⚫ get_intersection() will return alist after performing intersection operation between two lists

⚫ compare() will compare two linked lists.

Steps to follow.

• Create two list using insert()_node() function

• Display the created lists using display_list() function

• Perfrom union operation

• Perfrom intersection operation

• Display the results.

Program:

Here for STM32CubeIDE to perform operation, we split the program to three parts

1. main.c For main function declaration and calling the function

2 listh For header declaration :For function definition

3. list.c

main.c

#include listh

#define DEBUG MAIN int main()

int first_list_nodes = 0, second_list_nodes = 0, first_list_items= 0, second_list_iters = 0;

printf("Enter the number of Nodes in first List: ");

flush(stdout);

scanf("%d",&first_list_nodes); printf("Enter %d the elements: "first_list_nodes);

for(int i=0;i

Mush(stdout);

scanf("%d",&first_list_Items);

head insert_node(headfirst_list_items);

display_list(head));

printf('n Enter the number of Nodes in Second List"); fflush(stdout);

scanf("%d",&second_list_nodes);

printf("Enter %d the elements "second_list_nodes); for(int i=0;i

fflush(stdout);

scanf("%d",&second_list_Items);

head2 insert_node(head2second_list_Items),

1

display_list(head2); res_union = get_union(head,head2):

Wilde! DEBUG MAIN

printf('n Value of Head and Head?: %d %d headthead2);

#endif

#ifdef DEBUG MAIN

printf('n Value of Head) and Head2 after performing union: %d %d n'headhead2), printf("Value of res_union xd nres_union);

#endif

printf("Here is the result of union");

display_list(res union):

res, intersection-get_intersection(head),nead2); printf('n Value of Headl and Head2 after performing union d on headthiouz

Aitdef DEBUG MAIN

printf("value of res intersection dnres intersection).

#endi

print("Here is the result of intersection display stires intersection);

}

List.h

#ifndef LIST H #deline UST_H_

#include #include #include

Define the Node/ typedef struct node

int data

struct node 'next

Node

static Node "headthead2, head, head union head intersection "nownode "tail"res_union res intersection

Define the Function/ Node "insert_node(Node "head_rel, int n)

vold display list(Node "head_ref),

Node "get union(Node "head pointer Node "head pointer2),

Node "get Intarsection(Node "head pointertNode "head pointer2).

int compare(Node "head,int data);

#endif /USTEH/

List.c

#include Tisth

void display list(Node "head_ral)

#ilda! DEBUG

printf(Entering Display function');

#endif

if(head_ref== 0)

printf("List is empty."); }

else

tail = head_ref;

while(tail != 0)

printf("%d", tail->data);// prints the data of current node tail = tail->next;// advances the position of current node

} tail = newnode;

} #ifdef DEBUG

printf("Value of tail is %dn,tail);

#endif

}

Node "insert_node(Node "head_ref,int n)

printf("ninserting Elements at the beginning_n"); newnode = (Node) malloc(sizeof(Node));

printf("Address of created newnode%dn newnode): newnode->data = n; printf("Inserted value is %dn',newnode->data);

newnode->next = head_ref;

head ref newnode;

#ifdef DEBUG

printf("Head Address: %dn head_ref);

printf("Newnode Address: %dn newnode); printf("Exiting insert function");

#endif

return head_ref;

Node 'get_union(Node "head pointerl,Node *head_pointer2)

while(head pointerl != 0)

head_union = insert_node(head_union,head_pointerl->data); head pointerl= head_pointerl->next;

}

while(head_pointer2!=0)

if(!compare(head_union,head_pointer2->data)) head_union = insert_node(head_union,head_pointer2->data); head_pointer2 = head_pointer2->next; }

return head_union; }

int compare(Node "head,int data) {

while (head != NULL)

} if (head->data == data)

return 1;

head head->next; }

return 0; }

Node "get_intersection(Node "head_pointerl.Node "head_pointer2)

while (head pointer] != 0)

if (compare(head_pointer2, head_pointer->data)) head intersection insert_node(head_intersection.head pointerl->da head pointer! head_pointerl->next;

Output:

Enter the number of Nodes in first List: 5 Enter 5 the elements: 2 3 4 5 6

Inserting Elements at the beginning

Address of created newnode :12721208

Inserted value is :2

Inserting Elements at the beginning Address of created newnode:12721224

Inserted value is :3 Inserting Elements at the beginning..

Address of created newnode 12721240

Inserted value is:4

Inserting Elements at the beginning Address of created newnode:12721256

Inserted value is :5 Inserting Elements at the beginning.. Address of created newnode:12721272

Inserted value is :6 65432

Enter the number of Nodes in Second List:5

Enter 5 the elements: 4 5 678 Inserting Elements at the beginning... Address of created newnode:12719736

Inserted value is:4 Inserting Elements at the beginning Address of created newnode:12719752

Inserted value is :5

Inserting Elements at the beginning... Address of created newnade :12719768 Inserted value is :6

Inserting Elements at the beginning Address of created newnode :12719784

Inserted value is 7

Inserting Elements at the beginning

Address of created newnode :12719800 Inserted value is :8

87654

Inserting Elements at the beginning... Address of created newnode 12719816 Inserted value is :6

Inserting Elements at the beginning... Address of created newnode:12719832 Inserted value is :5

Inserting Elements at the beginning... Address of created newnode :12719848 Inserted value is :4

Inserting Elements at the beginning.. Address of created newnode :12719864 Inserted value is :3

Inserting Elements at the beginning Address of created newnode :12719880

Inserted value is :2

Inserting Elements at the beginning

Address of created newnode:12719896

Inserted value is :8 Inserting Elements at the beginning....

Address of created newnode:12719912

Inserted value is :7

Here is the result of union: 7823456 Inserting Elements at the beginning Address of created newnode:12742528 Inserted value is 6

inserting Elements at the beginning. Address of created newnode:12720400

inserted value is 5

Inserting Elements at the beginning... Address of created newnode:12720416

Inserted value is:4

Here is the result of intersection: 4 5 6

Coding :

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.

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.