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 <stdio.h>
# include <stdlib.h>
# include <stdbool.h>

// CREATING A STRUCTURE NODE
struct node
{
int value;//DATA
struct node *nxt;//POINTER TO LINK NEXT NODE
};
void insert(struct node** head,int data);
void displayLinkedlist(struct node *pt);
bool ispresent(struct node* head,int data);

// TO INSERT THE VALUES IN THE LINKED LISTS
void insert(struct node** head,int data)
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));//ALLOCATING THE NODE
newNode->value=data;//ASSINGNING VALUE
newNode->nxt =(*head);//LINK THE OLD LIST OF NEWNODE
(*head)=newNode;//MOVING THE HEAD TO POINT NEWNODE
}
//GETTING THE VALUES AND DISPLAYING IT
void displayLinkedlist(struct node *pt)
{
while(pt!=NULL)//WHEN POINTER IS NOT EQUAL TO NULL THEN LINKED LIST EMPTY
{
fflush(stdout);
printf("%d ",pt->value);//PRINTING THE VALUE
pt=pt->nxt;//POINTING THE POINTER TO LINK THE NEXT
}
}

/* TO PERFORM UNION OPERATION OF LIST 1 AND 2 */
struct node* uni(struct node* head1,struct node* head2)
{
struct node* outcome=NULL;
struct node *g1=head1,*g2=head2;
/*INSERT ALL VALUES OF LIST1 TO OUTCOME LIST*/
while (g1!=NULL)
{
insert(&outcome,g1->value);
g1=g1->nxt;
}
//INSERT THOSE VALUES OF LIST2 WHICH ARE NOT PRESENT IN OUTCOME
while (g2!=NULL)
{

if (!ispresent(outcome,g2->value))
{
insert(&outcome,g2->value);
}
g2=g2->nxt;

}
return outcome;
}

/* TO PERFORM INTERSECTION OPERATION OF LIST 1 AND 2 */
struct node* inter(struct node *head1,struct node *head2)
{
struct node* outcome=NULL;
struct node* g1=head1;
/*TRAVERSE LIST1 AND SEARCH EACH VALUE IN LIST2.
IF PRESENT IN LIST 2, THEN INSERT THE ELEMENT TO OUTCOME*/
while (g1!=NULL)
{
if (ispresent(head2,g1->value))
{
insert(&outcome,g1->value);
}
g1=g1->nxt;
}

return outcome;
}
/*FUNCTION TO CHECK THE PRESENCE OF VALUE ,RETURNS TRUE IF DATA IS
PRESENT IN LINKED LIST ELSE RETURN FALSE*/
bool ispresent(struct node* head,int data)
{
struct node* g=head;
while (g!=NULL)
{
if(g->value==data)
return 1;
g=g->nxt;
}
return 0;
}

int main()
{
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* union_operation = NULL;
struct node* inter_operation = NULL;
int i, LL1=0, LL2=0, n;
/* CREATING FIRST LINKED LIST*/

printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d",&LL1);
fflush(stdout);
printf("Enter %d integers\n", LL1);
for(i=0; i<LL1; i++)
{ fflush(stdout);
scanf("%d",&n);
insert(&head1,n);
}
printf("LINKED LIST1:\n");
displayLinkedlist(head1);

/* CREATING FIRST LINKED LIST*/

printf("\nEnter number of nodes in second Linked List\n");
fflush(stdout);
scanf("%d",&LL2);
fflush(stdout);
printf("Enter %d integers\n",LL2);
for(i=0; i<LL2; i++)
{ fflush(stdout);
scanf("%d", &n);
insert(&head2,n);
}
printf("LINKED LIST2:\n");
displayLinkedlist(head2);

union_operation=uni(head1, head2);//CALLING UNION FUNCTION
inter_operation=inter(head1, head2);//CALLING INTERSECTION FUNCTION
printf("\nThe union of LIST 1 and LIST 2:\n");
displayLinkedlist(union_operation);
printf("\nThe intersection of LIST 1 and LIST 2:\n");
displayLinkedlist(inter_operation);
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.