Challenge

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

#include

# include

# include

/* linked list node */

struct node

{

int data;

struct node*next;

}

/* a utility function to insert a node at the beginning of a linked list */

void push (struct node ** head-ref,int new-data);

/* a utility programme to check if given data is present in a list */

bool is present (struct node*head,int-data);

/* function to get union of two linked lists head1 and head 2*/

struct node*get union(struct node*head1,struct node*head2)

{

struct node* result=null;

struct node *t1=head1,t2=head2

//insert all elements of

//list1 to the result list

while(t1 |=null)

{

push(&result,t1->data);

t1=t1->next;

}

return result;

}

/* function toget intersection of two linked lists head1 and head2*/

struct node*get intersection (struct node*head1,struct node*head2)

{

struct node*result=null;

struct node*t1=head1

//traverse list1 and search each element of it in

//list2.if the element is present in list2,then

// insert the element to result

t1=t1->next;

}

return result

}

/* a utility function to insert a node at hte beginning of linked list*/

void push(struct node**head-ref,int new- data )

{

/*allocate node */

struct node* new-node=(struct node*)malloc (size of (struct node));

/*put in the data */

new_node->data=new_data;

/*put inthe data*/

new_node->next=(*head_ref);

/* move the head to point thenew node */

(*head_ref)=new_node;

}

/* a utility function to print a linked list */

void print list(struct node*node)

{

while (node|=null)

{

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

node=node->next;

}

}

/* a utility function that returns true if data is present in linked list else return false */

bool is present (struct node*head,int data)

{

struct node *t head;

while(t|=null)

{

if(t->data==data)

return 1;

t=t->next;

}

return 0;

}

/* driver program to test above function */

int main ()

{

/* start with the empty list */

struct node* head1=null;

struct node* head2=null;

struct node*interseen=null;

struct node*union=null;

/*ncreate a linked list 15->20->10->25 */

push(&head1,25);

push(&head1,4);

push(&head1,20);

push(&head1,15);

/* create a linked list 10->6->4->12 */

push(&head2,12);

push(&head2,4);

push(&head2,6);

push(&head2,10);

intersection =get intersecn (head1,head2);

union=get union (head1,head2);

printf("\n first list is \n");

print list(head1);

printf ("\n second list is \n);

print list (head2);

printf ("\n intersection list is \n");

print list (intersection);

printf ("\n union list \n");

print list (union);

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.